"Subscripted assignment dimension mismatch." error

Hi,
I'm trying to write this loop here and it's returning the error mentioned above. The matrix dimensions are:
287x16150 location
287x16150 cum_return
287x16150 index
1093x21 ME_Breakpoints
300x16150 Monthly_return
for k = 1:287 %for each month
location(k,:)=find(Cum_Return(k,:) > ME_Breakpoints(13+k,3));
[~,index(k,:)] = sort(Cum_Return(k,:), 'descend');
sorted_index(k,:) = intersect(index(k,:), location, 'stable');
portfolio_return= mean(Monthly_Return(:,index(k,1))-mean(Monthly_Return(:,index(k,1))))
end
Thanks!

3 件のコメント

Stephen23
Stephen23 2016 年 12 月 31 日
@Ming Au: please show us the complete error message. This means all of the red text.
Ming Au
Ming Au 2016 年 12 月 31 日
Hi Stephen, the title of this post was the whole error message:
"Subscripted assignment dimension mismatch."
the cyclist
the cyclist 2016 年 12 月 31 日
This is difficult to diagnose, because that error could have come from a couple different lines. Could you upload the actual data in a MAT file?

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2016 年 12 月 31 日

0 投票

Ming, you could have done what Stephen asked. Anyway, find() can return any number of elements - it varies. But when you do this:
location(k,:)=find(Cum_Return(k,:) > ME_Breakpoints(13+k,3));
you're trying to set all 16150 columns in row k. But what if find returns only 50 elements? It can't stuff 50 elements into 16150 elements. You have to stuff only as many as you have, which is 50. I've fixed that but the error occurs later also, so see if you can fix that yourself.
location = rand(287,16150);
Cum_Return = rand(287,16150);
index = rand(287,16150);
ME_Breakpoints = rand(1093,21);
Monthly_Return = rand(300, 16150);
for k = 1:287 %for each month
indexes = find(Cum_Return(k,:) > ME_Breakpoints(13+k,3));
numIndexes = length(indexes);
location(k,1:numIndexes) = indexes;
[~,index(k,:)] = sort(Cum_Return(k,:), 'descend');
sorted_index(k,:) = intersect(index(k,:), location(k,1:numIndexes), 'stable');
portfolio_return= mean(Monthly_Return(:,index(k,1))-mean(Monthly_Return(:,index(k,1))))
end

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 12 月 31 日

回答済み:

2016 年 12 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by