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

4 views (last 30 days)
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 Comments
Matt J
Matt J on 27 Jan 2023
Edited: Matt J on 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.

Sign in to comment.

Accepted Answer

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

More Answers (0)

Categories

Find more on Dynamic System Models in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!