Skipping the for loop if condition is meet

6 visualizzazioni (ultimi 30 giorni)
Prakhar Modi
Prakhar Modi il 20 Ago 2019
Modificato: Rik il 20 Ago 2019
Hello everyone,
for example,
a = 1, 2, 3,4,5
b=7,8,3,9,6
so i write code
for i=1:5
c(i)=a(i)-b(i)
if (a(i)==b(i))
% if this condition is satisfied i.e. as a(3)=b(3)
% so i want that c(3) should be skipped and then c(4) should be calculated
continue
end
end
So what should i write in place of % so that it will skip the c(3) iteration and continue to further calculation.
  1 Commento
Rik
Rik il 20 Ago 2019
Your code already seems to do that. What is your question? For this specific example, Torsten's code would work better.

Accedi per commentare.

Risposte (1)

Torsten
Torsten il 20 Ago 2019
idx = a~=b;
c(idx) = a(idx)-b(idx);

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by