Prev: need very quick help in grayscale image processing
Next: mex access to custom class object methods (mex + OOP) II
From: forkandwait w on 5 May 2010 16:03 Is there a *general* or standard way to extract a two dim matrix in which data is nested hierarchically along the rows into an N dim matrix? I am looking for a general solution, since I can do it ad hoc just fine. I have the following (best viewed in fixed width): x = yr1 yr2 yr3 yr4 yr5 county1/male 1 2 3 4 5 county1/fem 10 20 30 40 50 county2/male 6 7 8 9 10 county2/female 60 70 80 90 100 county3/male 11 12 13 14 15 county3/female 110 120 130 140 150 I would like to transform it into a 3 by 2 by 5 matrix: ans(:,:,1) = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ans(:,:,2) = 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
From: Walter Roberson on 5 May 2010 16:14 forkandwait w wrote: > Is there a *general* or standard way to extract a two dim matrix in > which data is nested hierarchically along the rows into an N dim > matrix? I am looking for a general solution, since I can do it ad hoc > just fine. > > I have the following (best viewed in fixed width): > > x = > yr1 yr2 yr3 yr4 yr5 county1/male 1 > 2 3 4 5 > county1/fem 10 20 30 40 50 > county2/male 6 7 8 9 10 > county2/female 60 70 80 90 100 > county3/male 11 12 13 14 15 > county3/female 110 120 130 140 150 > > I would like to transform it into a 3 by 2 by 5 matrix: In your example, male and female strictly alternate. Is that a certainty? If so, then once you have removed the leading header column (not sure the 'county1/male' part is real or just explanitory): cat(3,x(1:2:end,:),x(2:2:end)) If the 'county1/male' part is a real field and the data is not certain to already be interweaved in the order you want to extract it, then the matter becomes more complicated.
From: Doug Schwarz on 5 May 2010 16:17 forkandwait w wrote: > Is there a *general* or standard way to extract a two dim matrix in > which data is nested hierarchically along the rows into an N dim > matrix? I am looking for a general solution, since I can do it ad hoc > just fine. > > I have the following (best viewed in fixed width): > > x = > yr1 yr2 yr3 yr4 yr5 county1/male 1 > 2 3 4 5 > county1/fem 10 20 30 40 50 > county2/male 6 7 8 9 10 > county2/female 60 70 80 90 100 > county3/male 11 12 13 14 15 > county3/female 110 120 130 140 150 > > I would like to transform it into a 3 by 2 by 5 matrix: > ans(:,:,1) = > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > > ans(:,:,2) = > > 10 20 30 40 50 > 60 70 80 90 100 > 110 120 130 140 150 cols = 5; rows = 3; planes = 2; permute(reshape(x.',cols,planes,rows),[3 1 2]) where cols, rows and planes are what you desire in the final result. -- Doug Schwarz dmschwarz&ieee,org Make obvious changes to get real email address.
From: forkandwait w on 5 May 2010 19:18 > cols = 5; > rows = 3; > planes = 2; > permute(reshape(x.',cols,planes,rows),[3 1 2]) > > where cols, rows and planes are what you desire in the final result. Has any kind soul written a tutorial to explain this kind of matrix manipulation?
From: Doug Schwarz on 5 May 2010 21:56
In article <hrsubf$stl$1(a)fred.mathworks.com>, "forkandwait w" <forkandwait(a)gmail.com> wrote: > > cols = 5; > > rows = 3; > > planes = 2; > > permute(reshape(x.',cols,planes,rows),[3 1 2]) > > > > where cols, rows and planes are what you desire in the final result. > > Has any kind soul written a tutorial to explain this kind of matrix > manipulation? I'm not sure if I know how to teach it. To solve such problems it's important that you know how MATLAB stores the numbers in an array. The operation X(:) will turn any array X into a column vector without moving any numbers so that's a good way to explore and try things. Also, reshape will change the size of an array without moving anything. The operators .' (transpose) and permute (transpose is really just permute(X,[2 1]) ) will move numbers around so play with them and see what happens. Maybe it's possible to formalize what I did, but I just did it intuitively. It helps that I've been using MATLAB for more than 20 years. -- Doug Schwarz dmschwarz&ieee,org Make obvious changes to get real email address. |