Effacer les filtres
Effacer les filtres

How to indexing directly multiple row?

4 vues (au cours des 30 derniers jours)
balandong
balandong le 4 Déc 2017
Commenté : balandong le 4 Déc 2017
Dear all, I wonder how we can do direct indexing in compact form, for the following case? In this problem, i want to extract two rows for different columns. To achieve the objective, the following code was realize. But, the code is lengthy, and I wonder if we can simplify this.
Thanks in advance.
ShufleNperc=5;
randm_row_NUm=[randperm(ShufleNperc);randperm(ShufleNperc)]; % Row idx
randm_no=randi(50,ShufleNperc,ShufleNperc); % value to be extracted base on the row
vvvv=zeros(size(randm_row_NUm,1),ShufleNperc); % Prelocate memory
for x_v=1:ShufleNperc
vvvv(:,x_v)=randm_no((randm_row_NUm(:,x_v))',x_v);
end

Réponse acceptée

KL
KL le 4 Déc 2017
Modifié(e) : KL le 4 Déc 2017
use linear indices,
With your example,
colInd = repmat(1:size(randm_no,2),2,1);
linind = sub2ind(size(randm_no),randm_row_NUm(:),colInd(:));
vvvv_simple = randm_no(linind);
you can reshape it if you want,
vvvv_simple = reshape(vvvv_simple,size(vvvv))
the result is the same for both methods,
>> vvvv
vvvv =
48 48 33 38 3
8 22 43 33 5
>> vvvv_simple
vvvv_simple =
48 48 33 38 3
8 22 43 33 5
  1 commentaire
balandong
balandong le 4 Déc 2017
Hi KL, Thanks for the quick reply. Seems your solution ignore the usage of for loops ad more neat.
Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing 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