Reallocating a matrix based on a row of column numbers - works for some but not others?!
2 views (last 30 days)
Show older comments
Hello all, I would really appreciate any help on a problem I'm having.
I have a matrix of data points for 0-360deg for 454 measured data points (C2plotdata3), and a matrix (C2mode1plotgeom) of x and y points giving me the location of each measurement points (2x454). I have also created a matrix called "reorder", which is a 1x425 matrix. My aim is to remap the points into a different arrangement. Reorder tells me the new location of each point value in two new matrices (C2plotgeom4 and C2mode1pointgeom2). For example, the first three values in reorder are 444,395,271, meaning that the data in column 1 of C2plotdata3 and C2mode1pointgeom should go to column 444 of the new matrices, the data in column 2 should go to column 395, 3 to 271, etc...
Variables:
C2plotdata3 (360x454)
C2plotgeom (2x454)
reorder (1x425)
NB Reorder is shorter than the others (425 rather than 454) because not all the original data points are to be remapped.
Code:
for y=1:size(reorder,2)
C2mode1plotgeom2(1,(reorder(1,y)))=C2mode1plotgeom(1,y)
C2mode1plotgeom2(2,(reorder(1,y)))=C2mode1plotgeom(2,y)
C2mode1plotdata4(:,(reorder(1,y)))=C2mode1plotdata3(:,y);
end
Problem:
This works for the point geometries (C2mode1plotgeom2 is correct), but doesn't appear to for the large matrix. C2plotgeom4 is created, but the values do not match (e.g. those in columns 444,395,and 271 do not match those in 1,2,3). I have tried removing this last line from the loop and entering a y value manually, and this does seem to work, but I can't get it to work in the loop.
Does anyone have any ideas? I suspect it will be something simple but this has cost me a few days now.
Thanks in advance, Stu
PS. I'm using 2017a
0 Comments
Answers (1)
Star Strider
on 12 Jun 2017
The problem is most likely due to using the loop.
Try something like this:
FirstMatrix_1 = randi(99, 4, 6)
SecondMatrix_1 = randi(99, 2, 6)
reorder = randperm(6)
FirstMatrix_2(:,reorder) = FirstMatrix_1
SecondMatrix_2(:,reorder) = SecondMatrix_1
FirstMatrix_1 =
26 73 46 23 40 88
80 49 96 49 37 91
3 58 55 62 98 79
92 24 52 68 4 10
SecondMatrix_1 =
26 68 72 65 78 90
34 14 11 49 71 89
reorder =
4 3 1 6 2 5
FirstMatrix_2 =
46 40 73 26 88 23
96 37 49 80 91 49
55 98 58 3 79 62
52 4 24 92 10 68
SecondMatrix_2 =
72 78 68 26 90 65
11 71 14 34 89 49
Does this do what you want?
4 Comments
Star Strider
on 13 Jun 2017
@Stuart — Our pleasure.
@Guillaume — Thank you very much for your help. (UTC-6 here, so asleep then.)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
