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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by