Need help in indexing
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
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
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));
Weitere Antworten (0)
Siehe auch
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!