Making an array of coordinates from another array
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
dpb
il 11 Nov 2013
Modificato: dpb
il 11 Nov 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.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!