1

How can I reshape a 2d array to a 3d array with the last column being used as pages? All data found in array2d should be in pages

example:

array2d=[7,.5,12; ...
1,1,1; ...
1,1,1; ...
4,2,4; ...
2,2,2; ...
2,2,2; ...
3,3,3; ...
3,3,3; ...
3,3,3];

The first page in the array would be 7,.5,12; 1,1,1; 1,1,1;

The second page in the array would be 4,2,4; 2,2,2; 2,2,2;

The third page in the array would be 3,3,3; 3,3,3; 3,3,3;

This is a 9x3 array how can I get it to be a 9x3x? (not sure what this number should be so I placed a question mark as a place holder) multidimensional array?

What I'm trying to get is to have All the ones would be on one dimension/page all the two's would be another dimension/page etc... –

I tried reshape(array2d,[9,3,1]) and it's still a 9x3

4
  • you need to use repmat or give a simple minimal example Commented Jun 16, 2014 at 2:58
  • @natan What I'm trying to get is to have All the ones be on one dimension/page all the two's would be another dimension/page etc... – Commented Jun 16, 2014 at 3:08
  • 1
    I still dont understand, if you write a(:,:,1)=[1 2 ; 1 2] what will a(:,:,2) look like? Commented Jun 16, 2014 at 3:53
  • @natan: The OP has stacked 2D matrices within a larger 2D matrix. Each of these 2D matrices all contain a single number, so there is a 3 x 3 matrix of all 1s, a 3 x 3 matrix of all 2s, etc. The OP wishes to take each of these matrices and create a stacked 3D matrix where each slice corresponds to a 2D matrix of just this number only. As such, out(:,:,1) = ones(3,3), out(:,:,2) = 2*ones(3,3)... etc., given the above example. Commented Jun 16, 2014 at 5:29

4 Answers 4

3

Use permute with reshape -

N = 3;  %// Cut after every N rows to form a "new page"
array3d = permute(reshape(array2d,N,size(array2d,1)/N,[]),[1 3 2]) %// output
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming that each slice of your matrix is the same in dimensions, we can do this very easily. Let's call the number of rows and columns that each slice would have to be M and N respectively. In your example, this would be M = 3 and N = 3. As such, assuming array2d is of the above form, we can do the following:

M = 3;
N = 3; %// This is also simply the total number of columns we have,
       %// so you can do size(array2d, 2);
outMatrix = []; %// Make this empty.  We will populate as we go.

%// Figure out how many slices we need
numRows = size(array2d,1) / M;

for k = 1 : numRows
    %// Extract the k'th slice
    %// Reshape so that it has the proper dimensions
    %// of one slice
    sliceK = reshape(array2d(array2d == k), M, N);
    %// Concatenate in the third dimension
    outMatrix = cat(3,outMatrix,sliceK);
end

With your example, we thus get:

>> outMatrix

outMatrix(:,:,1) =

     1     1     1
     1     1     1
     1     1     1

outMatrix(:,:,2) =

     2     2     2
     2     2     2
     2     2     2

outMatrix(:,:,3) =

     3     3     3
     3     3     3
     3     3     3

This method should generalize for any number of rows and columns for each slice, provided that each slice shares the same dimensions.

6 Comments

when I change array2d=[7,3,7; ... 3,3,3; ... 3,3,3; ... 4,4,4; ... 4,4,4; ... 4,4,4; ... 2,2,2; ... 2,2,2; ... 2,2,2; ... 1,1,1; ... 1,1,1; ... 1,1,1]; I get an error "error: reshape: can't reshape 7x1 array to 3x3 array Are you taking the reshape directly out of the data values? if so is it possible not to reshape the array based on the data but the amount of rows/columns themselves?
@RickT: This won't work if you have 7,3,7 in there. It looks at the data itself. If you want this to change, please update your problem description accordingly.
@RickT: Do you want to ignore all those rows that don't follow the pattern? Still not quite clear.
@RickT - No problem. Divakar's solution is more suited towards your needs. I misunderstood your problem entirely. Good luck.
+1 for making the problem clearer through the outputs
|
0

Your array is already of size 1 in the 3rd dimension (in other words, it is already 9x3x1, to prove this try entering array2d(1,1,1)). If you want to concatenate 2d matrices along the 3rd dimension you can use cat.

For example:

a = [1,2;3,4];
b = [5,6;7,8];
c = cat(3,a,b);

c will be a 2x2x2 matrix.

2 Comments

I guess what I meant to say is that all the ones would be on one dimension/page all the two's would be on another dimension/page etc...
The way you have array2d arranged in the example, you could do cat(3,array2d(1:3,:),array2d(4:6,:),array2d(7:9,:)). If your pages are of constant height, you could pretty easily turn this into a for loop.
0

This piece of code is specific for this example, I hope you will be able to understand how to go for other data samples.

out2 = [];
col = size(array2d,2);
for i = 1:3        
    temp2 = reshape(array2d(array2d == i),[],col);
    out2 = cat(3,out2,temp2);
end

6 Comments

The 0 will cause your matrix to be empty as 0 isn't a valid size for the third dimension. Also, out2 in the for loop should be out. I also tried running this code and it's giving me an error when you're trying to concatenate. The reason why it's failing is how you are declaring out. It should be zeros(3,3) for this example to work. In addition, this code will not work if col is anything but 3.
@rayryeng Thanks for pointing out the errors, sorry I did not followed your advice and did the same mistake again, I have corrected it, mistake was to declare out2 instead of out. And I believe it will work for different column size given that each block with value 1 , 2 , 3 has same size
The first line will still give you problems. You are declaring a 9x3x0 array, which will give you the empty matrix. In addition, should you remove the 0, it will generate a 9 x 3 matrix to start off. When you try to concatenate the matrices, in the loop it will give you 3x3 matrices, when you initially started off with a 9x3 matrix. The cat command will throw an error due to inconsistent sizes when concatenating.
You can avoid this cat error if you make the matrix initially empty, or out2 = []; What you did above is that you are declaring an empty matrix of 9x3x0 when it should simply be empty to start with.
@rayryeng thanks , I corrected it now. Again sorry for repeating the mistake. I know I have said sorry so many times that it may seem like I don't mean it, but please believe me I do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.