why am I getting "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" in this code?

3 views (last 30 days)
for i=1:x_row ,m=1:magic_row;
for j=1:x_col , n=1:magic_col;
if(a<=c)
% t=x(i,j)+y(m,n);
if((Str(a)+x(i,j)+y(m,n))>255)
temp=x(i,j)+Str(a)+y(m,n)-256;
else
temp=x(i,j)+Str(a)+y(m,n);
end
z(i,j)=uint8(temp); %this line shows error
else
z(i,j)=uint8(x(i,j));
end
a=a+1;
end
end
  2 Comments
Stephen23
Stephen23 on 10 Mar 2018
@Ishvarya E: please show us the complete error message. This means all of the red text. Currently we have no idea where the error occurs.
Ishvarya E
Ishvarya E on 11 Mar 2018
Edited: Ishvarya E on 11 Mar 2018
@Stephen Cobeldick: Thank you. And this is the error message I got.
"Subscripted assignment dimension mismatch.
Error in encrypt (line 32) z(i,j)=uint8(temp);"

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Mar 2018
Your lines
for i=1:x_row ,m=1:magic_row;
for j=1:x_col , n=1:magic_col;
are equivalent to
for i=1:x_row
m=1:magic_row;
for j=1:x_col
n=1:magic_col;
so your m and n are going to end up being vectors, and y(m,n) is going to be a 2D array. That leads to temp being a 2D array, but you are trying to store temp into the single location z(i,j)
There is no way in MATLAB to use a single for loop to change two variables simultaneously. Since your two variables per line appear to be different length, they appear to be varying independently, so you will probably need to go for four nested loops -- or else vectorize.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!