How do i get the index position of a multiple number from an array?
3 次查看(过去 30 天)
显示 更早的评论
Hi My array is
A=[300 500 900 100 1800 34 400 600 2700 450]
I want to get the index of the multiple of 900 i.e 900,1800,2700
Index=find(A==900)
Any way of modifying it to get the index of the multiples?
0 个评论
回答(1 个)
per isakson
2015-7-3
编辑:per isakson
2015-7-3
Seems to do it
>> is = arrayfun( @(num) mod(num,900)==0, A )
is =
0 0 1 0 1 0 0 0 1 0
>> A(is)
ans =
900 1800 2700
>> ix = find(is)
ix =
3 5 9
>>
or better
>> is = mod( A, 900 ) == 0
is =
0 0 1 0 1 0 0 0 1 0
Answer:
Index = find( mod(A,900) == 0 );
6 个评论
per isakson
2015-7-3
编辑:per isakson
2015-7-3
I didn't read, however this might do it.
>> [ix1,ix2] = cssm
ix1 =
1 4 7
ix2 =
3 6 9
where
function [ix1,ix2] = cssm
B = [ 0 4 8 12 16 20 24 28 32 36 40 43 ];
ix1 = 1;
ix2 = [];
while true
ix = find( B(ix1(end):end)==B(ix1(end))+8, 1,'first' ) + ix1(end)-1;
if isempty(ix)
break
end
ix2(end+1) = ix;
ix1(end+1) = ix+1;
end
ix1(end) = [];
end
There is certainly potential for improvements.
另请参阅
类别
在 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!