minimum point in nested for loop and using min function..i want to know the value of x for which every row of y has minimum elements please help
1 view (last 30 days)
Show older comments
clear;clc;close;
u1=1:1:3;
u2=0:0.1:1.1;
for jj=1:length(u1)
s1=inf;
for kk=1:length(u2)
x(jj,kk)=u1(jj)+u2(kk);
y=abs(x.^2-2);
z=min(y,[],2);
end
end
0 Comments
Accepted Answer
Voss
on 8 Apr 2024
clear;clc;close;
u1=1:1:3;
u2=0:0.1:1.1;
for jj=1:length(u1)
% s1=inf;
for kk=1:length(u2)
x(jj,kk)=u1(jj)+u2(kk);
end
end
x
% simpler than nested loops:
x = u1.'+u2
y=abs(x.^2-2);
[z,c]=min(y,[],2);
r = 1:numel(z);
idx = sub2ind(size(x),r,c.');
x_min = x(idx)
More Answers (0)
See Also
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!