How to add a row vector for indexing

2 vues (au cours des 30 derniers jours)
Ashraf Sherif
Ashraf Sherif le 12 Fév 2019
Commenté : Ashraf Sherif le 13 Fév 2019
How to add a row vector for indexing C by M when my M is 4 dimensional ? any help will be thankful.
F=random('exp',1,3,3,4);
C=F*100
M=bsxfun(@times, C./cumsum(C,3) , reshape(1:4,1,1,[]) );
[vec,idx] = max(M,[],1); % value and row index of max in each column.
S = size(M); % size of input array.
idx = idx + S(1)*(0:S(2)-1); % convert row index into linear index !
C(idx); % use linear index to get elements of C.
  2 commentaires
Jan
Jan le 12 Fév 2019
What exactly is your question? What do you want to add to what? Does the posted code do what you expect? If not, what do you want to change?
Ashraf Sherif
Ashraf Sherif le 12 Fév 2019
thank you Jan for inquires, well I have a matrix C is a 3 by 3 random variables with 4 iterations, at each iterate each element devided by it is average and collected at matrix M which done by below code
M=bsxfun(@times, C./cumsum(C,3) , reshape(1:4,1,1,[]) );
I want to index C by the maximum values in each column of matrix M, by this code
[vec,idx] = max(M,[],1);
so far all are OK but to continue the indexing by using the below code
idx = idx + S(1)*(0:S(2)-1);
which to convert row index into linear index, an error appears
Error using +
Matrix dimensions must agree.
Error in Untitled5 (line 26)
idx = idx + S(1)*(0:S(2)-1);
some told me I have to add a row because different dimensions but i couldn't do that, and I am looking for some hlep or if there are an other methode to do whole this operation.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 12 Fév 2019
Modifié(e) : Matt J le 12 Fév 2019
I would do it this way,
idx=bsxfun(@eq, M, max(M,[],1) );
C(idx),
  2 commentaires
Matt J
Matt J le 12 Fév 2019
Modifié(e) : Matt J le 12 Fév 2019
I would also recommend that you learn how to use BSXFUN since you are running into it again and again, and since apparently you cannot upgrade to a more recent Matlab version where it is less required.
Ashraf Sherif
Ashraf Sherif le 13 Fév 2019
Thank you very much Matt J for you advice and help

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by