Making an array of coordinates from another array
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2013-11-11
编辑:dpb
2013-11-11
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!