How can I reduce the for loop number?
Show older comments
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 Comment
Stephen23
on 27 Jun 2021
Accepted Answer
More Answers (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 Comment
ASHFAQ AHMED
on 27 Jun 2021
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!