i have a code as below, it changes signal matrix (3 dimension) to responses matrix (4 dimension). i had expected to have dimension error in line 6 but it did not. can some one explain why? thank you

for epoch=1:size(Signal,1)

rowcolcnt=ones(1,12);

for n=2:size(Signal,2)

if Flashing(epoch,n)==0 && Flashing(epoch,n-1)==1

rowcol=StimulusCode(epoch,n-1);

responses(rowcol,rowcolcnt(rowcol),:,:)=Signal(epoch,n-24:n+window-25,:);

rowcolcnt(rowcol)=rowcolcnt(rowcol)+1;

end

end

end

Answer by James Tursa
on 5 Sep 2019

To solve your sizing issues, type the following into MATLAB:

dbstop if error

Then run your code. When the error occurs, your code will pause at the offending line with all variables still intact. Examine them so see which ones are causing the problem. Then backtrack in your code to figure out why the sizes are not what you expected. E.g., you should be looking at the following:

size(responses)

size(Signal)

rowcol

rowcolnt(rowcol)

epoch

n

window

Sign in to comment.

Answer by Guillaume
on 5 Sep 2019

i had expected to have dimension error in line 6 but it did not

I would expect an error if the if condition is true when n <= 24, since the n-24 would then be an invalid index for Signal.There would also be an error if n+window-25 is greater than size(signal, 2).

But otherwise, I would not expect any error. You're putting a 2D slice into another 2D slice. I would expect however lots of 0s in your response matrix due to the way its constructed.

To show you how the matrix is filled, let's assume that response does not exist yet, that epoch is 1, window is 10 and the if becomes true for n = 30. At that step, let's assume StimulusCode returns 6, the assignment is thus:

Response(6, 1, :, :) = Signal(1, 6:15, :);

which means that

Response(1, 1, :, :) to Response(1, 1, :, :) gets filled with 0s. Response(6, 1, 1, :) gets filled with Signal(1, 6, :), Response(6, 1, 2, :) gets filled with Signal(1, 7, :), etc. Response(6, 1, 10, :) gets filled with Signal(15, :).

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## James Tursa (view profile)

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/478975-matrix-dimension-reshape-error#comment_742554

## Guillaume (view profile)

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/478975-matrix-dimension-reshape-error#comment_742556

Sign in to comment.