Reallocating a matrix based on a row of column numbers - works for some but not others?!

1 vue (au cours des 30 derniers jours)
Stuart Walker
Stuart Walker le 12 Juin 2017
Commenté : Star Strider le 13 Juin 2017
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

Réponses (1)

Star Strider
Star Strider le 12 Juin 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 commentaires
Stuart Walker
Stuart Walker le 13 Juin 2017
Modifié(e) : Stuart Walker le 13 Juin 2017
Thanks Guillaume. That was my mistake, I wrote 455 instead of 454 when I typed the code. However, the other mistake was bigger - I was using 'reorder' (the original matrix I created yesterday) instead of 'testreorder', so my results and that rest of my post are incorrect. I would edit or delete my comment but this does not appear to be allowed.
Anyway, working in the same way you did, the rearrangement now works correctly with a test reordering matrix. It does not work with the reordering matrix I want to use, but this must now be the root of the problem so I will focus on this and try to find out what is really the problem.
Thanks to you and Star Strider for your help- apologies for wasting your time with my incorrect code.
Stuart
Star Strider
Star Strider le 13 Juin 2017
@Stuart — Our pleasure.
@Guillaume — Thank you very much for your help. (UTC-6 here, so asleep then.)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by