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!