Need help in indexing
3 views (last 30 days)
Show older comments
Ashraf Sherif
on 11 Feb 2019
Commented: Ashraf Sherif
on 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 Comments
Accepted Answer
Walter Roberson
on 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 Comments
Walter Roberson
on 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));
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!