How to use operations in for loop problems
조회 수: 2 (최근 30일)
이전 댓글 표시
For values of 1 to 60, create a for-loop that does the following. If the value is 1 to 20, have
the operation be number2, if the value is 21 to 40, have the operation be number, and if the
value is 41 to 60, have the operation be √number. Store all 60 results in 3 different variables
that start at 1. With the results, create a 3x20 matrix where the first row is the results of the
first operation, the second row being the results of the second operation, and the final row
being the results of the final operation
댓글 수: 1
Steven Lord
2024년 5월 2일
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
채택된 답변
Joshua Levin Kurniawan
2024년 5월 3일
For this task, you can use for loop as follows
A = zeros(3,20);
for ii = 1:60
if ii<=20
A(1,ii) = ii^2; %first operation
elseif ii<=40
A(2,ii-20) = ii; %second operation
else
A(3,ii-40) = sqrt(ii); %third operation
end
end
disp(A)
댓글 수: 1
Walter Roberson
2024년 5월 3일
We do not recommend giving away the complete answer to homework problems.
추가 답변 (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!