alternative form for find(max(x))

1 visualización (últimos 30 días)
GCats
GCats el 4 de Abr. de 2020
Editada: dpb el 4 de Abr. de 2020
Hi all!
I am trying to optimize my program by reducing the nested coding and perhaps "slow" functions such as find().
I have this:
gamma = 0.5
nodes = 5
if rand >= gamma
a = find(A(s(1),s(2),:) == max(A(s(1),s(2),:)),1);
else
a = randi(nodes);
end
A is a n x m x o matrix.
I was wondering if there's a different way to tackle this. I've looked at accum() but I don't seem to make it work properly. Thank you in advance!

Respuesta aceptada

dpb
dpb el 4 de Abr. de 2020
if rand >= gamma
[~,imx]=max(A(s(1),s(2),:);
a=imx;
else
a = randi(nodes);
end
  2 comentarios
Torsten
Torsten el 4 de Abr. de 2020
Is it ensured that imx gives the index of the first occurence of the maximum within A(s1,s2,:) ?
dpb
dpb el 4 de Abr. de 2020
Editada: dpb el 4 de Abr. de 2020
I've never caught it doing otherwise altho not sure it is documented...
ADDENDUM: Actually it is...
"If the largest element occurs more than once, then I contains the index to the first occurrence of the value."

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by