필터 지우기
필터 지우기

Replace all numbers within a matrix

조회 수: 1 (최근 30일)
Krish Desai
Krish Desai 2015년 9월 27일
답변: Star Strider 2015년 9월 27일
I'm trying to change all the numbers in a matrix between 1: n^2
My code is:
function output = IsMagic(n)
A= zeros(n);
r=1;
c=1;
row_index = 0;
while row_index <= r
col_index=0;
while col_index <= c
A(r,c) = randi(1*n^2);
col_index=col_index + 1;
end
row_index= row_index +1;
end
output=A;
end
  댓글 수: 1
Stephen23
Stephen23 2015년 9월 27일
편집: Stephen23 2015년 9월 27일
I formatted your code for you, but in future please do it yourself by selecting the code text and clicking the {} Code button that you will find above the text box.
Please do not put blank lines between every line of your code.

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

채택된 답변

Star Strider
Star Strider 2015년 9월 27일
I’m not quite certain I understand what you want to do. This will produce a (n x n) matrix with your random integers:
A= zeros(n);
r=n;
c=n;
row_index = 1;
while row_index <= r
col_index=1;
while col_index <= c
A(row_index,col_index) = randi(1*n^2);
col_index=col_index + 1;
end
row_index= row_index +1;
end
One problem is that you set ‘r’ and ‘c’ both to 1, so you were only ever addressing A(1,1). I changed those to n, and the row and column references for ‘A’ to ‘row_index’ and ‘col_index’. I also started those at 1 instead of 0. The code now produces the matrix I believe you want to create. Change the code as necessary so it does what you want.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by