Matrix compare row to column

조회 수: 13 (최근 30일)
Eva Carrillo
Eva Carrillo 2019년 12월 3일
답변: Raj 2019년 12월 3일
I just learned how to do matrices today, and am quite confused on this problem. So I have to create a 100 x 100 matrix. Every spot in the matrix takes on the value , where i represents the row and j represents the column location. I have a formula in which the value K is defined as the greater value of i and j. I need to create an if loop that compares the row and column position values and chooses the larger of the two, then defines that as the variable K.
I'm very new to this and have searched around, but nothing seems to lead me in the right path. Help would be a ppreciated!

답변 (1개)

Raj
Raj 2019년 12월 3일
Okay lets try to break it down here:
1) I have to create a 100 x 100 matrix
Create an all zero matrix like this:
A=zeros(100,100)
2) I have a formula in which the value K is defined as the greater value of i and j
Pretty sure this is what you must have in mind:
if i>j
k=i
else
k=j
end
3) compares the row and column position values and chooses the larger of the two, then defines that as the variable K
So you have to check all the values in the matrix A for above condition. You can do this easily by nested loops like this:
for i=1:100
for j=1:100
%Do your check here
end
end
Finally your code should look something like this:
A=zeros(100,100)
for i=1:100
for j=1:100
if i>j
k=i
else
k=j
end
A(i,j)=k;
end
end
Is this what you are looking for?

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by