"Subscripted assignment dimension mismatch." error
Show older comments
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 Comments
Stephen23
on 31 Dec 2016
@Ming Au: please show us the complete error message. This means all of the red text.
Ming Au
on 31 Dec 2016
the cyclist
on 31 Dec 2016
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?
Answers (1)
Image Analyst
on 31 Dec 2016
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
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!