Need Help Creating a loop
조회 수: 2 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
답변 (1개)
David Hill
2021년 3월 17일
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);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!