MATLAB Answers

Error using + Matrix dimensions must agree

1 view (last 30 days)
sam
sam on 15 Nov 2013
Commented: sam on 15 Nov 2013
Error using + Matrix dimensions must agree.
Error in AddWaveform (line 8) tmp=tmp+Wav(((i-1)*2090+x):i*2090);
1 function tmp = AddWaveform(Wav)
2 tmp=zeros(1,2090);
3 for i=1:1244
4 x=1;
5 flag=0;
6 while(flag==0)
7 if (mod(Wav((i-1)*2090+x),7500)==0)
8 tmp=tmp+Wav(((i-1)*2090+x):i*2090);
9 flag=1;
10 end
11 x=x+1;
12 end
13 end
my input waveform value is 1x2601900 double

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 15 Nov 2013
x keeps growing larger and larger until eventually (i-1)*2090+x exceeds 2601900.

  4 Comments

Show 1 older comment
sam
sam on 15 Nov 2013
I see, thanks for the answer Image.
However, in my case, it's not possible for x to exceed 2601900, because it will definitely flag before that. Why is this not a warning, but an error?
How do you think I should fix this?
Image Analyst
Image Analyst on 15 Nov 2013
When it fails, one of the indexes is too big. Try a function like this:
function tmp = AddWaveform(Wav)
try
tmp=zeros(1,2090);
for i=1:1244
x=1;
flag=0;
while(flag==0)
if (mod(Wav((i-1)*2090+x),7500)==0)
index1 = ((i-1)*2090+x);
index2 = i*2090;
tmp=tmp+Wav(((i-1)*2090+x):i*2090);
flag=1;
end
x=x+1;
end
end
catch ME
errorMessage = sprintf('Error in AddWaveform().\nThe error reported by MATLAB is:\n\n%s', ME.message);
uiwait(warndlg((errorMessage));
end
Set a breakpoint in the catch block on the uiwait() statement, and see what index1 and index2 are when it stops there. And see what (i-1)*2090+x evaluates to. One of them will be too big.
sam
sam on 15 Nov 2013
-Image,
thanks so much. It was that with my current code, Wav(((i-1)*2090+x):i*2090) was not the same size as tmp.
I only figured it out when I viewed the difference between Index1 and Index2. So basically, I was trying to loop and add a different sized matrix each time.
thanks again!

Sign in to comment.

More Answers (0)