필터 지우기
필터 지우기

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.

조회 수: 1 (최근 30일)
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

채택된 답변

John D'Errico
John D'Errico 2016년 3월 15일
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.
  댓글 수: 2
Neha W
Neha W 2016년 3월 15일
Thank you. I will definitely try to implement using meshgrid.
John D'Errico
John D'Errico 2016년 3월 15일
Something like this would be a start:
[rr,cc] = meshgrid(1:r,1:c);
A = [rr(:),cc(:)];
I'd need to check to see if it generates them in the sequence you asked for, but it will be close.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by