How to create a Aij matrix with the commands 'for', 'function output=name_of_code' and 'imagesc'?

1 Ansicht (letzte 30 Tage)
I have to built a Aij matrix where Aij=i^2+j^2 when i=j and Aij=(i-j)^2 when i≠j. After that, I need to execute the code with m=100 and n=150. My teacher said the useful commands are 'for', 'function output=name_of_code' and 'imagesc'. I don't know how to do it and I didn't find any example. I write the code: for A=i^2+j^2
i=j
end
for A=(i-j)^2
i~=j
end
This appear on the command window: i =
0.0000 + 1.0000i
ans =
0
I don't know if what I've done is good. After I write m=[1:100]
n=[1:150]
A=[m,n]
imagesc(A)
function [A]=matrice_A
end
for A=i^2+j^2
i=j
end
It gives me this figure. I don't know if it is good but it doesn't look like a matrix.
  2 Kommentare
Matt J
Matt J am 27 Jan. 2023
Bearbeitet: Matt J am 27 Jan. 2023
Ariane wrote:
I have to built a Aij matrix where Aij=i^2+j^2 when i=j and Aij=(i-j)^2 when i≠j. After that, I need to execute the code with m=100 and n=150. My teacher said the useful commands are 'for', 'function output=name_of_code' and 'imagesc'. I don't know how to do it and I didn't find any example. I write the code: for A=i^2+j^2
i=j
end
for A=(i-j)^2
i~=j
end
This appear on the command window: i =
0.0000 + 1.0000i
ans =
logical
0
I don't know if what I've done is good. After I write m=[1:100]
n=[1:150]
A=[m,n]
imagesc(A)
function [A]=matrice_A
end
for A=i^2+j^2
i=j
end
It gives me this figure. I don't know if it is good but it doesn't look like a matrix.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 27 Jan. 2023
m=100; n=150;
A=((1:m)'-(1:n)).^2;
for i=1:min(m,n)
A(i,i)=2*i.^2;
end
imagesc(A)

Weitere Antworten (0)

Kategorien

Mehr zu Dynamic System Models finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by