Subdividing column matrix based on two stored indices of different lengths
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a column matrix 'A' that could be any length with any value and a coresponding column matrix of same length with whole number values within. I would like to pull values from 'A' into a new column matrix 'B' based on a column matrix of any length 'pull' where the values I pull correspond to the number in 'order rather an the row indices. I recognize this may be a confusing explanation so see the indended output in the coding section. What is a good way to do this? I can see how looping and checks could work but I would like to avoid loops if possible.
A = [0.0048;-0.001;-0.0042;0.0047;-0.00011;0.0025;0;0;0;0;0.0023;0.0004];
order =[4;5;6;7;8;9;1;2;3;10;11;12];
pull = [7;8;9;10;11;12];
%intended output
B = [0.0047;-0.00011;0.0025;0;0.0023;0.0004];
alternative values for 'pull' are shown below.
pull = [1;2;3;4;5;6];
pull = [1;7];
0 Kommentare
Antworten (1)
dpb
am 21 Apr. 2023
Bearbeitet: dpb
am 21 Apr. 2023
A = [0.0048;-0.001;-0.0042;0.0047;-0.00011;0.0025;0;0;0;0;0.0023;0.0004];
order =[4;5;6;7;8;9;1;2;3;10;11;12];
pull = [7;8;9;10;11;12];
[~,ib]=ismember(pull,order);
B=A(ib)
I think your example B above is in error for the last three locations...they're the last three locations in the order array.
5 Kommentare
dpb
am 21 Apr. 2023
Bearbeitet: dpb
am 21 Apr. 2023
OK, if order is important, then use (updated Answer above as well; tried to avoid the temporary ib as order wasn't specifically required in original...although I suppose it could have been inferred from the answers proposed).
A = [0.0048;-0.001;-0.0042;0.0047;-0.00011;0.0025;0;0;0;0;0.0023;0.0004];
order =[4;5;6;7;8;9;1;2;3;10;11;12];
pull = [[1;2;3;4;5;6] [7;8;9;10;11;12]];
for i=1:size(pull,2)
[~,ib]=ismember(pull(:,i),order);
B(:,i)=A(ib);
end
B.'
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!