matrix dimension reshape error

조회 수: 2 (최근 30일)
Ramin
Ramin 2019년 9월 5일
답변: Guillaume 2019년 9월 5일
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
James Tursa
James Tursa 2019년 9월 5일
Please post code as the raw text only so that we can copy & paste directly into MATLAB at our end.
Guillaume
Guillaume 2019년 9월 5일
I've edited the post to make it more readable. Removing the line numbers was a pain!

댓글을 달려면 로그인하십시오.

답변 (2개)

James Tursa
James Tursa 2019년 9월 5일
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 2019년 9월 5일
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, :).

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by