Compare elements from two matrix.

2 vues (au cours des 30 derniers jours)
Nikita Zyk
Nikita Zyk le 13 Mai 2020
Commenté : Nikita Zyk le 15 Mai 2020
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

Réponse acceptée

Tommy
Tommy le 13 Mai 2020
How about this?
x = x1;
idx = abs(x1) < abs(x2);
x(idx) = x2(idx);
  6 commentaires
Tommy
Tommy le 14 Mai 2020
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 le 15 Mai 2020
It's good! Thank you so much! ;)

Connectez-vous pour commenter.

Plus de réponses (1)

Olawale Ikuyajolu
Olawale Ikuyajolu le 13 Mai 2020
new_matrix = max(abs(x1),abs(x2);
  3 commentaires
Olawale Ikuyajolu
Olawale Ikuyajolu le 13 Mai 2020
Nikita Zyk
Nikita Zyk le 13 Mai 2020
Unfortunetly, it has less efficiency. ;(

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by