MATLAB Answers

matrix dimension reshape error

2 views (last 30 days)
Ramin
Ramin on 5 Sep 2019
Answered: Guillaume on 5 Sep 2019
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
  2 Comments
Guillaume
Guillaume on 5 Sep 2019
I've edited the post to make it more readable. Removing the line numbers was a pain!

Sign in to comment.

Answers (2)

James Tursa
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

Guillaume
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, :).

Community Treasure Hunt

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

Start Hunting!