"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 个评论

@Ming Au: please show us the complete error message. This means all of the red text.
Hi Stephen, the title of this post was the whole error message:
"Subscripted assignment dimension mismatch."
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 个)

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 Exchange 中查找有关 Matrix Indexing 的更多信息

提问:

2016-12-31

Community Treasure Hunt

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

Start Hunting!

Translated by