Compare elements from two matrix.

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

 Respuesta aceptada

Tommy
Tommy el 13 de Mayo de 2020
How about this?
x = x1;
idx = abs(x1) < abs(x2);
x(idx) = x2(idx);

6 comentarios

Nikita Zyk
Nikita Zyk el 14 de Mayo de 2020
Editada: Nikita Zyk el 14 de Mayo de 2020
Yeah, it's good but I have 3% efficiency. Still don't understand why. Maybe you can help me?
I have to find roots of quadratic equation ax^2 + bx + c = 0 (it has to be well done for all values of a, b, c), where a,b,c - vectors, and x contains elements which absolute value is the smallest one (compare x1 and x2).
I wrote this:
function x = minroot(a,b,c)
end
if (b < 0)
x1 = 2.*c./(-b + sqrt(b.^2 - 4.*a.*c));
x2 = (-b + sqrt(b.^2 - 4.*a.*c))./2./a;
else
x1 = (-b - sqrt(b.^2 - 4.*a.*c))./2./a;
x2 = 2.*c./(-b - sqrt(b.^2 - 4.*a.*c));
end
x = x1;
idx = abs(x1) > abs(x2);
x(idx) = x2(idx);
Nikita Zyk
Nikita Zyk el 14 de Mayo de 2020
Forgot to add that I have testing program that gives me the percent of efficiency and accurancy. This code has 100% accurancy, but only 3% efficiency.
Tommy
Tommy el 14 de Mayo de 2020
3%? Hmm...
Well if b is an array, then
if (b < 0)
is asking whether all elements in b are less than 0. But I believe the two equations for x1 are equivalent, as are the two equations for x2 (as long as a and c are non-zero), so I don't really see the point of the if statement or the alternate form.
Does something like this improve the efficiency?
function x = minroot(a,b,c)
sdel = sqrt(b.^2 - 4*a.*c);
x = (-b - sdel)./(2*a);
x2 = (-b + sdel)./(2*a);
idx = abs(x) > abs(x2);
x(idx) = x2(idx);
end
If there is a possibility that one of the elements in a could be 0, then you would need to handle that case appropriately. One option would be to use the alternate form (provided c is not also 0); another would be to recognize that the sole root in that case is then -c/b. Either way, I don't see how the sign of b would matter...
Nikita Zyk
Nikita Zyk el 14 de Mayo de 2020
Yes, but accuracy wil be lesser. Why? Because of the lack of if statement for b: if b<0, so -b+sdel can effect the result, because for b~sdel we subtract two numbers that are really closer one to another.
Tommy
Tommy el 14 de Mayo de 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 el 15 de Mayo de 2020
It's good! Thank you so much! ;)

Iniciar sesión para comentar.

Más respuestas (1)

Olawale Ikuyajolu
Olawale Ikuyajolu el 13 de Mayo de 2020
new_matrix = max(abs(x1),abs(x2);

3 comentarios

Nikita Zyk
Nikita Zyk el 13 de Mayo de 2020
And in new_matrix there will be abs(x), not x. I need x.
Nikita Zyk
Nikita Zyk el 13 de Mayo de 2020
Unfortunetly, it has less efficiency. ;(

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Mayo de 2020

Comentada:

el 15 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by