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 个评论
Josh
Josh 2013-11-11
Cheers Steve. I had just found that as you answered. :)
dpb
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by