MATLAB Answers

Matrix dimensions must agree error

1 view (last 30 days)
Marius Tøndel Eliassen
Marius Tøndel Eliassen on 19 Feb 2020
I am sure I used this exact code yesterday without problems (line 27)
Matrix dimensions must agree.
Error in abs_threshold_colorationuser (line 27)
m_test(start_idx:end_idx) = m_test(start_idx:end_idx) + 10^(M.VAR/20)*y2;
Error in mpsy_check (line 208)
eval([M.EXPNAME 'user']);
Error in mpsy_afc_main (line 60)
mpsy_check;
Error in abs_threshold_coloration (line 71)
mpsy_afc_main;
Marius

  0 Comments

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 19 Feb 2020
First, do not use eval. It's not the cause for your problem here but it's a very dangerous tool which typically makes it harder to debug your code. It's also completely unneeded here,
M.([EXPNAME, 'user']); %and use , to separate expressions in [], the lack of , is also a typical source of bugs.
would work just as well.
As for the error, we don't have enough information to say what the problem is exactly, most likely the size of m_test(start_idx:end_idx) is not the same as the size of 10^(M.VAR/20)*y2 and the most likely reason for that is that y2 doesn't have end_idx-start_idx+1 elements or if it has, is not a vector. Since we don't know the size of the variables, their shape, or where they come from, we can't help you further.

  3 Comments

Marius Tøndel Eliassen
Marius Tøndel Eliassen on 19 Feb 2020
That code is adding signal vector y2, attenuated by M.VAR, to test interval m_test, which already consists of y1. Y2and y1 are exactly the same size. So it then must be that start_idx and end_idx is not of equal size?
This is the code that specify start_idx and end_idx:
start_idx = round((y1_dur-y2_dur)*M.FS/2)+2;
end_idx = start_idx + round(y2_dur*M.FS)-2;
Guillaume
Guillaume on 19 Feb 2020
As I said, the most likely reason is that the length of your vector y2 is not exactly equal to end_idx-start_idx+1. Certainly, the round in the calculation of start_idx and end_idx are suspicious. Are you sure you haven't got an off-by-one error?
You can always add:
assert(numel(y2) == end_idx-start_idx+1, 'Mismatch between length of y2 (%d) and calculated bounds (%d:%d = %d elements)', numel(y2), start_idx, end_idx, end_idx-start_idx+1);
before line 27 which will at least warn you of the problem but of course won't solve it.
Marius Tøndel Eliassen
Marius Tøndel Eliassen on 19 Feb 2020
I (think) i know root of the problem now, but I don't know how to specify the change correctly.
For 1 number of channels the code should be:
start_idx = round((y1_dur-y2_dur)*M.FS/2)+1;
end_idx = start_idx + round(y2_dur*M.FS)-1;
and in this case end_idx equals the FS (sampling frequency), which is 44100.
For 2 number of channels end_idx much exceeds FS, and therefore result in Matrix dimension error I guess

Sign in to comment.

Tags

No tags entered yet.

Products


Release

R2019b