Subscripted assignment dimension mismatch.

3 次查看(过去 30 天)
I have 10 .mat file that are 1x4916 each that I want to plot on the same graph, I have this code
load('ErrorProb_OrthTr_Gray3Orth1');
load('ErrorProb_OrthTr_Gray3Orth2');
load('ErrorProb_OrthTr_Gray3Orth3');
load('ErrorProb_OrthTr_Gray3Orth4');
load('ErrorProb_OrthTr_Gray3Orth5');
load('ErrorProb_OrthTr_Gray3Orth6');
load('ErrorProb_OrthTr_Gray3Orth7');
load('ErrorProb_OrthTr_Gray3Orth8');
load('ErrorProb_OrthTr_Gray3Orth9');
load('ErrorProb_OrthTr_Gray3Orth10');
size1 = size(ErrorProb_OrthTr_Gray3Orth1);
size12 = size1+size(ErrorProb_OrthTr_Gray3Orth2);
size123 = size12+size(ErrorProb_OrthTr_Gray3Orth3);
size1234 = size123+size(ErrorProb_OrthTr_Gray3Orth4);
size12345 = size1234+size(ErrorProb_OrthTr_Gray3Orth5);
size123456 = size12345+size(ErrorProb_OrthTr_Gray3Orth6);
size1234567 = size123456+size(ErrorProb_OrthTr_Gray3Orth7);
size12345678 = size1234567+size(ErrorProb_OrthTr_Gray3Orth8);
size123456789 = size12345678+size(ErrorProb_OrthTr_Gray3Orth9);
size12345678910 = size123456789+size(ErrorProb_OrthTr_Gray3Orth10);
AllErrProb(1:size1)= ErrorProb_OrthTr_Gray3Orth1(:);
AllErrProb(size1+1:size12)= ErrorProb_OrthTr_Gray3Orth2(:);
AllErrProb(size12+1:size123)= ErrorProb_OrthTr_Gray3Orth3(:);
AllErrProb(size123+1:size1234)= ErrorProb_OrthTr_Gray3Orth4(:);
AllErrProb(size1234+1:size12345)= ErrorProb_OrthTr_Gray3Orth5(:);
AllErrProb(size12345+1:size123456)= ErrorProb_OrthTr_Gray3Orth6(:);
AllErrProb(size123456+1:size1234567)= ErrorProb_OrthTr_Gray3Orth7(:);
AllErrProb(size1234567+1:size12345678)= ErrorProb_OrthTr_Gray3Orth8(:);
AllErrProb(size12345678+1:size123456789)= ErrorProb_OrthTr_Gray3Orth9(:);
AllErrProb(size123456789+1:size12345678910)= ErrorProb_OrthTr_Gray3Orth10(:);
plot(AllErrProb,'b')
hold on
plot(mean(AllErrProb),'-r')
xlabel('Orthogonal Transform Index')
ylabel('Error Probability')
but when I compile I get this error
Subscripted assignment dimension mismatch.
Error in Comparator____Exc (line 24)
AllErrProb(1:size1)= ErrorProb_OrthTr_Gray3Orth1(:);
can someone help ?

回答(2 个)

Geoff Hayes
Geoff Hayes 2018-7-13
Aziz - size1 could be an array (depending upon the dimensions of ErrorProb_OrthTr_Gray3Orth1) so that is going to lead to a problem when doing the assignment at
AllErrProb(1:size1)= ErrorProb_OrthTr_Gray3Orth1(:);
Try using numel instead as
size1 = numel(ErrorProb_OrthTr_Gray3Orth1);
to get the number of elements of ErrorProb_OrthTr_Gray3Orth1. Then your assignment will have a better chance of working.
You may also want to pre-allocate memory to AllErrProb and consider an alternative to the above size variables (perhaps put all of your data in a cell array).
  2 个评论
Tijani Aziz Feki
Tijani Aziz Feki 2018-7-16
Hi Geoff, I get this response from my supervisor
The Matlab code is assigning values to the AllErrProb, so you do NOT load it from somewhere else. The error says that the dimensions do not match. This means the onlz thing you have to is for each line where a value is assigned rather than size1, size2, etc. you should check the size of the right hand side and write an index range on the left hand side with the same size.
what he mean by index range on the left hand side ?
Geoff Hayes
Geoff Hayes 2018-7-17
编辑:Geoff Hayes 2018-7-17
Tijani - I'm not sure what your supervisor means. Perhaps
[dim1,dim2] = size(ErrorProb_OrthTr_Gray3Orth1);
and then use the right-hand value, dim2? But that is just a guess... Please see size for more details.

请先登录,再进行评论。


Dennis
Dennis 2018-7-17
You need to pay attention when using size. Even if your data is a vector size will return 2 values unless you specify a dimension.
size1 = size(ErrorProb_OrthTr_Gray3Orth1);
this will return 1 4916, now if you use a vector for indexing only the first entry will be considered:
AllErrProb(1:size1)= ErrorProb_OrthTr_Gray3Orth1(:);
% 1:1 =4916 entries ...does not really fit in
If i get your code right you are trying to concatenate all your vectors (i hope they really are), in that case i want to suggest a different approach:
AllErrProb=[];
for i=1:10
S=load(fullfile(pwd,['errorProb_OrthTr_Gray3Orth',num2str(i)]));
name=fieldnames(S);
AllErrProb=[AllErrProb,S.(name{1})];
end
A few words on a different matter: naming variables size1, size 12, size 123 is in general a bad idea. Often you want to access their values in a loop and it gets harder if you did not index your values properly. size1(1), size1(2), size1(3) would be a better way. I can recommend to have a look at Stephen Cobeldick's tutorial on this matter: https://de.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by