How can I reduce the for loop number?
Afficher commentaires plus anciens
How can I express these two for loops using only one single for loop?
x_data = [2 6 7 9; 4 9 11 14];
y_data = [8 10 16 31; 15 18 21 24];
for i = 1:length(x_data)
x(i) = [sum((x_data(1,i))-(x_data(2,i)))].^2;
i = i+1;
end
disp(x)
for j = 1:length(y_data)
y(j) = [sum((y_data(1,j))-(y_data(2,j)))].^2;
j = j+1;
end
disp(y)
1 commentaire
Stephen23
le 27 Juin 2021
Réponse acceptée
Plus de réponses (1)
No loops necessary.
x_data = [2 6 7 9; 4 9 11 14];
y_data = [8 10 16 31; 15 18 21 24];
x = diff(x_data,1,1).^2
y = diff(y_data,1,1).^2
1 commentaire
ASHFAQ AHMED
le 27 Juin 2021
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!