Subdividing column matrix based on two stored indices of different lengths

2 visualizzazioni (ultimi 30 giorni)
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];

Risposte (1)

dpb
dpb il 21 Apr 2023
Modificato: dpb il 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)
B = 6×1
0.0047 -0.0001 0.0025 0 0.0023 0.0004
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 Commenti
Aaron DeSantis
Aaron DeSantis il 21 Apr 2023
I think there may be a misunderstanding with the intent behind my origional question.The goal is to find a way to take the given inputs:
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];
and extract individual elements of A based on an array of a different length. For example the two shown below.
pull_1 = [1;2;3;4;5;6];
pull_2 = [7;8;9;10;11;12];
The key here is that the ouputs 'B_1' and 'B_2' must me in the same order as "pull". Meaning the intended outputs are:
B_1 = [0;0;0;0.0048;-0.001;-0.0042];
B_2 =[0.0047;-0.00011;0.0025;0;0.0023;0.0004];
For clarity I will provide another example going the other way:
If I want to get the output shown:
B_3 = [0.0048;-0.001;-0.0042;0;0;0];
I expect the pull_3 array used to be:
pull_3 = [4;5;6;1;2;3];
I need the outputs to be in the order because they will be used in further code. I hope this clears up any misundestanding and I apologise if you found the question misleading in any way.
dpb
dpb il 21 Apr 2023
Modificato: dpb il 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.'
ans = 2×6
0 0 0 0.0048 -0.0010 -0.0042 0.0047 -0.0001 0.0025 0 0.0023 0.0004

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by