How can I find the first constant in an array per row that matches a certain absolute value?
Mostrar comentarios más antiguos
I'm trying to find exit times from a simulation. My results are saved in an array/matrix. Each row represents the values of my 1-D random walk. I'm trying to find how long it takes on average to reach some point k.
I first created a smaller array to play with first, and tried using the function
[CODE] [i, j] = find( abs(test_array) == 9); [i, j] [\CODE]
So this gives me a two-column array, with the first column being my row and second column being the position in the row at which abs(9) occurs. But I only want to find the very first occurrence per row. Looking through the documentation I found the 'first' argument, so I tried:
[CODE] [i, j] = find(abs(test_array) == 9, 5, 'first') [\CODE] (for an array with 5 rows. That didn't work though. I also tried transposing it and running the above, but since it goes down each column, it finds multiple points per column rather than just one per column (when transposed) or one per row (as I originally wanted).
Respuesta aceptada
Más respuestas (2)
the cyclist
el 19 de Sept. de 2013
Instead of
find(abs(test_array) == 9, 5, 'first')
you want
find(abs(test_array) == 9, 1, 'first')
because you want the just 1 instance, not the first 5.
See
doc find
for details.
2 comentarios
Michael
el 19 de Sept. de 2013
the cyclist
el 19 de Sept. de 2013
My bad. I did not read your question carefully enough. Will mull a bit more.
Kelly Kearney
el 19 de Sept. de 2013
You can do this with accumarray:
[ii,jj] = find(abs(test_array) == 9);
col = accumarray(ii, jj, [size(x,1) 1], @(x) min(x));
Though it may be more legible to just loop over the rows.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!