# Subdividing column matrix based on two stored indices of different lengths

1 vue (au cours des 30 derniers jours)
Aaron DeSantis le 21 Avr 2023
Modifié(e) : dpb le 21 Avr 2023
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 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

dpb le 21 Avr 2023
Modifié(e) : dpb le 21 Avr 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 commentairesAfficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
dpb le 21 Avr 2023
Modifié(e) : dpb le 21 Avr 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

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by