"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
2016년 12월 31일
@Ming Au: please show us the complete error message. This means all of the red text.
Ming Au
2016년 12월 31일
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
2016년 12월 31일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!