Comparing elements in one matrix in a loop
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ancalagon8
le 18 Sep 2018
Modifié(e) : Ancalagon8
le 18 Sep 2018
Hi All,
I have a large matrix with one column containing only values. I want to make a loop and compare every time if the average of 2nd and 1st value is < than a predifined value. When the average of i and j value is > than the predifined value, to show as a result i and j. Thanks in advance!
0 commentaires
Réponse acceptée
KALYAN ACHARJYA
le 18 Sep 2018
Modifié(e) : KALYAN ACHARJYA
le 18 Sep 2018
A=randi(30,20,1); %Random Matrix for Example
th=12; %Some Predefined value
[rows colm]=size(A);
for i=1:rows-1
c=(A(i,1)+A(i+1,1))/2;
if c > th
fprintf('The i is: %d and the next row i+1 value is: %d \n',i,i+1);
break;
end
end
12 commentaires
KALYAN ACHARJYA
le 18 Sep 2018
Modifié(e) : KALYAN ACHARJYA
le 18 Sep 2018
The first one only case. I have edited the answer, the added break statement. Please check.
Other c condition also possible, can you clarify more.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!