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

1 vue (au cours des 30 derniers jours)
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 commentaires
Matt J
Matt J le 27 Jan 2023
Déplacé(e) : Matt J le 27 Jan 2023
I don't know how to do it and I didn't find any example.
Not even here?
Matt J
Matt J le 27 Jan 2023
Modifié(e) : Matt J le 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.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 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)

Plus de réponses (0)

Catégories

En savoir plus sur Dynamic System Models dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by