필터 지우기
필터 지우기

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 (최근 30일)
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

채택된 답변

Voss
Voss 2024년 4월 8일
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>

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by