Need help in indexing

조회 수: 3 (최근 30일)
Ashraf Sherif
Ashraf Sherif 2019년 2월 11일
댓글: Ashraf Sherif 2019년 2월 15일
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.

채택된 답변

Walter Roberson
Walter Roberson 2019년 2월 12일
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
Walter Roberson
Walter Roberson 2019년 2월 14일
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 2019년 2월 15일
Thank you a lot Walter Roberson

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by