Clear Filters
Clear Filters

Need Help Creating a loop

3 views (last 30 days)
Giovanni Principato
Giovanni Principato on 17 Mar 2021
Answered: David Hill on 17 Mar 2021
I am kind of new at creating matlab loops and just needed help on this question.
Use loops to create a m x n Matrix named Mat in which the value of each element is the sum of its row number and its column number divided by the square of its column number. For example, the value of element (2,3) is (2+3)/3^2.
m is the number of rows and n is the number of columns. Code has already been provided to assign m and n psuedo-random integer values between 5 and 15. Suppress all statements inside the loop and use a disp command to output the final matrix after the loop.
I am given this:
m = randi([5,15]);
n = randi([5,15]);
%Create a m x n matrix using for loop.
%The elements has to be (row number+column number)/(square of column number)
I have created this and idk how to create it into a matrix.
Mat = [m,n]
[R,C] = size(Mat)
for row = 1:R
for col = 1:C
Mat(row,col) = Mat((R+C)/C^2,(C+R)/C^2)
end
end
disp(Mat)

Answers (1)

David Hill
David Hill on 17 Mar 2021
m = randi([5,15]);
n = randi([5,15]);
mat=zeros(m,n);
for row=1:m
for col=1:n
mat(row,col)=(row+col)/col^2;
end
end
display(mat);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!