필터 지우기
필터 지우기

Subscripted assignment dimension mismatch

조회 수: 1 (최근 30일)
Shane McNamara
Shane McNamara 2017년 10월 25일
댓글: Shane McNamara 2017년 10월 26일
Can anyone highlight why i am getting this error in the below code please?
domainSize = [50 50];
domain = zeros(domainSize);
domain(24:26,24:26) = 1;
% Generate Position Arrays
[particlePosition(:,1), particlePosition(:,2)] =find(domain);
for i = 1:length(particlePosition)
%Select Direction to Move
switch ceil(4 * rand)
case 1
dR = [-1 0];
case 2
dR = [+1 0];
case 3
dR = [0 -1];
case 4
dR = [0 +1];
end
%New Particle Location
tempPosition = particlePosition + dR;
%Move Particle
particlePosition(i,:) = tempPosition;
end

채택된 답변

Roger Stafford
Roger Stafford 2017년 10월 25일
There are quite a few things wrong with this code.
1) In “for i = 1:length(particlePosition)” you will get only three values of i from 1 to 3, but you have nine “particles” to move.
2) The part of the code that begins with “%New Particle Location” is located outside the for-loop so only the last “particle” is moved.
3) The line “tempPosition = particlePosition + dR;” attempts to add ‘particlePosition’, which is a 9 x 2 matrix to ‘dR’, which is only a 1 x 2 vector. Naturally Matlab will object strenuously to such an ill-advised attempt. This is undoubtedly the source of your error message.
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 10월 25일
Note: since R2016b, adding a 9 x 2 and a 1 x 2 will work, and will be the same as if you had use bsxfun() to do the addition.
Shane McNamara
Shane McNamara 2017년 10월 26일
Thanks for your help :D

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by