How to solve : Subscripted assignment dimension mismatch error

조회 수: 15 (최근 30일)
bahar kh
bahar kh 2017년 5월 26일
댓글: Andrei Bobrov 2017년 5월 26일
Dear guys I am new in MATLAB. It is a simple code which i make it. but i get this error message"Subscripted assignment dimension mismatch." I dont know whats the problem with that. would you please help me?
A=[]
A=randi(10,2,3)
for i=1:2
for j=1:3
if A(i,j)>3
A(i,j)='ok';
elseif A(i,j)==2
A(i,j)='no'
else
A(i,j)=' ';
end
end
end
  댓글 수: 1
Stephen23
Stephen23 2017년 5월 26일
편집: Stephen23 2017년 5월 26일
A is a numeric array, which you then try to force non-scalar char vector into the elements of. It is not possible to put multiple array elements into one element of another array. If r and c are scalars, then:
X(r,c) = 1; % okay
X(r,c) = [1,2] % an error

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

채택된 답변

bahar kh
bahar kh 2017년 5월 26일
still I have a question.when I change the code to the following form it works too.why? the last code and new one, almost are the same. but why the original code in my question doesn't work but the modified version works? what is the difference between them? thanks in advance
the modifiied version is here A=[] A=randi(10,2,3) for i=1:2 for j=1:3 if A(i,j)>3 A(i,j)=0; elseif A(i,j)==2 A(i,j)=2 else A(i,j)=6; end end end
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2017년 5월 26일
>> A=randi(10,2,3)
A =
9 2 7
10 10 1
>> A(1,1) = 2
A =
2 2 7
10 10 1
>> A(2,1) = 'no'
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
>> double('no')
ans =
110 111
>>

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 5월 26일
Use cell - array ( Aout) for output data in your function
A=randi(10,2,3);
Aout = cell(size(A);
for i=1:2
for j=1:3
if A(i,j)>3
Aout{i,j}='ok';
elseif A(i,j)==2
Aout{i,j}='no'
else
Aout{i,j}=' ';
end
end
end
  댓글 수: 1
bahar kh
bahar kh 2017년 5월 26일
편집: bahar kh 2017년 5월 26일
thanks@Andrei Bobrov. It works for me. but still I have a question.when I change the code to the following form it works too.why? the last code and new one, almost are the same. but why the original code in my question doesn't work but the modified version works? what is the difference between them? thanks in advance
the modifiied version is here A=[] A=randi(10,2,3) for i=1:2 for j=1:3 if A(i,j)>3 A(i,j)=0; elseif A(i,j)==2 A(i,j)=2 else A(i,j)=6; end end end

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by