How would I write a script to solve this problem. Assume that I have a vector named D. Using iteration (for) and conditionals (if and/or switch), separate vector D into four vectors posEven, negEven, posOdd, and negOdd.

6 visualizaciones (últimos 30 días)
*Find the maximum, minimum elements and get the summation and so on, but how would I solve the problem using conditional statement and iterations instead.***
a. posEven contains all of the positive even numbers in D b. negEven contains all of the negative even numbers in D c. posOdd contains all of the positive odd numbers in D d. negOdd contains all of the negative odd numbers in D
  3 comentarios
Bryce Imbriale
Bryce Imbriale el 28 de Sept. de 2018
Editada: Walter Roberson el 28 de Sept. de 2018
This is what i have so far but problebly looks super confusing.
>> D = [-4,-3,-2,-1,0,1,2,3,4]
posEven = []; posOdd = []; negEven = []; negOdd = [];
for val = D
if val > 0
if mod(val,2) == 0
posEven = [posEven val]
else
posOdd = [posOdd val]
end
elseif val < 0
if mod(val,2) == 0
negEven = [negEven val]
else
negOdd = [negOdd val]
end
end
end
D =
Columns 1 through 8
-4 -3 -2 -1 0 1 2 3
Column 9
4
negEven = -4 -2
negOdd = -3 -1
posOdd = 1 3
posEven = 2 4

Iniciar sesión para comentar.

Respuestas (1)

OCDER
OCDER el 28 de Sept. de 2018
Editada: OCDER el 28 de Sept. de 2018
My guess is Bryce needs to "find the maximum and minimum elements, and get the summation" for each variable. But seems max, min, or sum cannot be used.
Start with:
maxPosEven = posEven(1);
for j = 2:numel(posEven)
if posEven(j) > maxPosEven %Adjust the inner contents for your case
maxPosEven = posEven(j);
end
end

Categorías

Más información sobre Matrix Indexing 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