How to Check if array has multiples of a value and how to replace those values that are multiples with a certain assigned values

13 vues (au cours des 30 derniers jours)
This is the Question:
  1. Check each value in the Z array to see if it is a multiple of 8. If an element is a multiple of 8, assign -6 to that element.
  2. The only thing you are to display in the Command window is the Z array values.
T
if mod(Z(m),8)==0 && mod(Z(m),8)==0
Z(m,n)=-6
end
Z
ans=
4 6 8 0
6 8 10 0
8 10 12 42
0 0 105 -6

Réponse acceptée

KSSV
KSSV le 29 Oct 2020
If A is your matrix.
idx = mod(A,8)==0 ; % get indices which are divisible by 8
A(idx) = -6 ; % repalce the values
  9 commentaires
KSSV
KSSV le 29 Oct 2020
Coding is easy...don't think your school forced you to take it. Try to learn and enjoy.
Thanks is accepting/ voting the answer. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by