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
댓글 수: 0
채택된 답변
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
% 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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!