Finding repeating values in an array
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Vladimir Kostic
el 25 de Abr. de 2020
Comentada: Rik
el 27 de Abr. de 2020
Hello all,
I have 2 arrays
A = [ 0.3 0.6 1 0.6 0.3]
B = [ 3 2 3 6 11 ]
I need to find the position of same elements in B and then the max value of the elements on the corresponding position in A.
In this case the number 3 is repeated in B on positions 1 and 3 so the corresponding values in A are 0.3 and 1 => max( 0.3 , 1 ) = 1
The end resault should be:
A1 = [ 0.6 1 0.6 0.3 ]
B1 = [ 2 3 6 11 ]
Any help is appreciated
3 comentarios
dpb
el 25 de Abr. de 2020
The find part is easy enough, the logic of how to build the A1, B1 vectors from A,B and the lookups escapes me entirely, though...???
Respuesta aceptada
aleksa markovic
el 25 de Abr. de 2020
Editada: aleksa markovic
el 25 de Abr. de 2020
You ca try something like this:
Xa = [3 2 3 6 11];
mua = [.3 .6 1 .6 .3];
tmpX = [];
tmpmu = [];
for i = 1:size(Xa,2)
if(sum(tmpX == Xa(i)) > 0)
tmpmu(tmpX == Xa(i)) = max(mua(i),tmpmu(tmpX == Xa(i)));
else
tmpX = [tmpX Xa(i)];
tmpmu = [tmpmu mua(i)];
end
end
Xa = tmpX;
mua = tmpmu;
0 comentarios
Más respuestas (2)
andrea
el 25 de Abr. de 2020
maybe i do not understand the problem but anyway
[val, ind] = min ( A ( pos_in_b) )
A(ind) = []
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!