필터 지우기
필터 지우기

Compare elements from two matrix.

조회 수: 1 (최근 30일)
Nikita Zyk
Nikita Zyk 2020년 5월 13일
댓글: Nikita Zyk 2020년 5월 15일
Hi!
I want to compare elements for two matrix and then create another matrix with maximal element (comparing abs(x1) i abs(x2), not x1 i x2).
I wrote this:
But maybe it's possible to do it quicker and more efficient?
for i = 1:numel(x1)
if (abs(x1(i)) > abs(x2(i)))
x(i) = x2(i);
else
x(i) = x1(i);
end
end

채택된 답변

Tommy
Tommy 2020년 5월 13일
How about this?
x = x1;
idx = abs(x1) < abs(x2);
x(idx) = x2(idx);
  댓글 수: 6
Tommy
Tommy 2020년 5월 14일
Ah okay thank you for the explanation!
It's not very pretty, but how well does this do?
function x = minroot(a,b,c)
sdel = sqrt(b.^2 - 4*a.*c);
idx = b < 0;
x(idx) = 2*c(idx)./(-b(idx) + sdel(idx));
x(~idx) = (-b(~idx) - sdel(~idx))./(2*a(~idx));
x2(~idx) = 2*c(~idx)./(-b(~idx) - sdel(~idx));
x2(idx) = (-b(idx) + sdel(idx))./(2*a(idx));
idx = abs(x) > abs(x2);
x(idx) = x2(idx);
end
Nikita Zyk
Nikita Zyk 2020년 5월 15일
It's good! Thank you so much! ;)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Olawale Ikuyajolu
Olawale Ikuyajolu 2020년 5월 13일
new_matrix = max(abs(x1),abs(x2);
  댓글 수: 3
Olawale Ikuyajolu
Olawale Ikuyajolu 2020년 5월 13일
Nikita Zyk
Nikita Zyk 2020년 5월 13일
Unfortunetly, it has less efficiency. ;(

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by