Filter löschen
Filter löschen

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 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Voss
Voss am 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>

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by