Clear Filters
Clear Filters

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)
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

Accepted Answer

Voss
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
x = 3x12
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% simpler than nested loops:
x = u1.'+u2
x = 3x12
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y=abs(x.^2-2);
[z,c]=min(y,[],2);
r = 1:numel(z);
idx = sub2ind(size(x),r,c.');
x_min = x(idx)
x_min = 1x3
1.4000 2.0000 3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

More Answers (0)

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!