Making an array of coordinates from another array

1 visualización (últimos 30 días)
Josh
Josh el 11 de Nov. de 2013
Editada: dpb el 11 de Nov. de 2013
I have a simple 1-d array with zeros and ones that looks something like:
array1=[1001011001]
Now I want to find the coordinates of where there is a '1' and insert those coordinates into an array.
I was thinking something along the lines of
for k=(1:10)
if array1(1,k)==1
k=x
end
end
But it doesn't seem to be liking this.
Any ideas? Cheers in advance.
  3 comentarios
Josh
Josh el 11 de Nov. de 2013
Cheers Steve. I had just found that as you answered. :)
dpb
dpb el 11 de Nov. de 2013
Editada: dpb el 11 de Nov. de 2013
And for nonzero elements in your example case the default condition is all that's needed.
idx=find(array1);
BTW, the problem in your loop solution is you're trying to use the array loop index as the target and there is no 'x'. It would look sotoo
j=0;
for i=1:length(array1)
if array1(i)==1
j=j+1;
idx(j)=i
end
end
This works but has a couple of issues the biggest of which is that the array idx isn't preallocated so that if it grows to be very large you'll see a large runtime penalty as the reallocations become more and more costly.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by