Borrar filtros
Borrar filtros

Need Help Creating a loop

3 visualizaciones (últimos 30 días)
Giovanni Principato
Giovanni Principato el 17 de Mzo. de 2021
Respondida: David Hill el 17 de Mzo. de 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)

Respuestas (1)

David Hill
David Hill el 17 de Mzo. de 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);

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by