How to use operations in for loop problems

2 visualizaciones (últimos 30 días)
Erick
Erick el 2 de Mayo de 2024
Comentada: Walter Roberson el 3 de Mayo de 2024
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 comentario
Steven Lord
Steven Lord el 2 de Mayo de 2024
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.

Iniciar sesión para comentar.

Respuesta aceptada

Joshua Levin Kurniawan
Joshua Levin Kurniawan el 3 de Mayo de 2024
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)
Columns 1 through 18 1.0000 4.0000 9.0000 16.0000 25.0000 36.0000 49.0000 64.0000 81.0000 100.0000 121.0000 144.0000 169.0000 196.0000 225.0000 256.0000 289.0000 324.0000 21.0000 22.0000 23.0000 24.0000 25.0000 26.0000 27.0000 28.0000 29.0000 30.0000 31.0000 32.0000 33.0000 34.0000 35.0000 36.0000 37.0000 38.0000 6.4031 6.4807 6.5574 6.6332 6.7082 6.7823 6.8557 6.9282 7.0000 7.0711 7.1414 7.2111 7.2801 7.3485 7.4162 7.4833 7.5498 7.6158 Columns 19 through 20 361.0000 400.0000 39.0000 40.0000 7.6811 7.7460
  1 comentario
Walter Roberson
Walter Roberson el 3 de Mayo de 2024
We do not recommend giving away the complete answer to homework problems.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by