user34790
user34790

Reputation: 2036

Reshaping a 3D data in matlab

I have this 3-D data in matlab of dimension 3x3x10. What I want is to reshape it to a data of size/dimension 10x9. Such that for each i,j,1:10. I have one column in this new data. How can I do it. I tried using reshape(data, 10,9). and it gave me a data structure of size 10x9. However, I doubt how it arranged it.

I want to reshape it just that if new_data is my new data of size 10x9, my first column is old_data(1,1,:). The second column of my new data is old_data(1,2,:) and so on

Upvotes: 1

Views: 220

Answers (2)

dsgrnt
dsgrnt

Reputation: 393

I personally abhor the use of for loops in Matlab. I think a better way would be to permute the dimensions, then reshape. Also, it is not clear the order in which you want to arrange the data. You should be able to achieve what you want by fiddling with the dimension numbers in the permute function call.

% B is a 3x3x10, permute to 10x3x3, then reshape into 10x9
% Change the ordering of the dimension in permute to achieve the desired result
% remember: reshape takes elements out of B column by column.

newB = reshape(permute(B,[3 1 2]),10,9);

% newB is a 10x9 matrix, where each row is the entries of the 3x3 matrix
% the first column is element 1:9:82

Upvotes: 1

pabaldonedo
pabaldonedo

Reputation: 957

You could do this:

for i = 1:size(x,3)
    x(:,:,i) = x(:,:,i).';
end
newData = reshape(x,[],size(x,3)).'

Upvotes: 0

Related Questions