MATLAB: I have written a code to assign each co-ordinate of matrix A(5 x 5) as A(1,1) = 1; A(1,2) = 2; and so on till A(5,5) = 25. But the code is giving errors.

for loop

I need to store it in an array using loops like [1 2 ….25]. The below code is giving me, "Expression is incorrect–possibly unbalanced (, {, or [." k = 1; for i = 1:r; for j = 1:c; A(k,:) = (i,j); k = k + 1; end end

Best Answer

The line:
A(k,:) = (i,j);
is not valid MATLAB syntax. Perhaps you wanted to write
A(k,:) = [i,j];
I cannot know if that is what you really want.
Of course, you can replace the loops completely using tools like meshgrid of ndgrid.