Need help in indexing

4 Ansichten (letzte 30 Tage)
Ashraf Sherif
Ashraf Sherif am 11 Feb. 2019
Kommentiert: Ashraf Sherif am 15 Feb. 2019
Hi all,
I want to index C by M matrixs as below but I have problem in convert row index into linear index code, I am Using MATLAB R2013a, can any one help me,Thank you in advance
close all
clear all
clc
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.
This are the error message
Error using +
Matrix dimensions must agree.
Error in Untitled6 (line 10)
idx = idx + S(1)*(0:S(2)-1); % convert row index into linear index.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Feb. 2019
Your M is 4 dimensional . The idx output of max along the first dimension is going to be 4 dimensional . You are trying to add a row vector to it.
  3 Kommentare
Walter Roberson
Walter Roberson am 14 Feb. 2019
baseidx = idx + repmat((0:size(M,2)-1) * size(M,1),1,1,size(idx,3)) + repmat(reshape((0:size(M,3)-1) * size(M,1)*size(M,2),1,1,[]),1,size(idx,2),1);
rowidx = baseidx + repmat(reshape((0:size(M,1)-1),[],1),1,size(M,2),size(M,3));
Ashraf Sherif
Ashraf Sherif am 15 Feb. 2019
Thank you a lot Walter Roberson

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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!

Translated by