How do i get the index position of a multiple number from an array?

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?

답변 (1개)

per isakson
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

Awesome! I have one more question. I do not know whether the mod function can be used. Assumming I have an array like
B=[0 4 8 12 16 20 24 28 32 36 40 43]
I want to get the starting and ending index for a multiples of 8.
For example
Value=0 Index=1 % stating index
Valye=8 Index=3 %ending index
Value=12 Index=4 %starting index
Value=20 Index=6 %ending index
Value=24 Index=7 %starting index
Vale=32 Index=9 %ending index
Please note the last number is not multiple of 8.
I don't get it! 12 and 20 are not multiples of 8.
I am sorry for the confusion. Actually, you right. It is not multiples. It is increment by the value 8.
For example, starting with the first number, I get the index and find the index of the next number which is an increment of 8. Again, start with the next number, then find the index of the next number which is an increment of 8.
Value=0 Index=1 % stating index
(increment of 8)
Value=8 Index=3 %ending index
Value=12 Index=4 %starting index
(increment of 8)
Value=20 Index=6 %ending index
Value=24 Index=7 %starting index
(increment of 8)
Vale=32 Index=9 %ending index
Any way we can get the the starting and ending index values for a general case ?
In simple terms, given the array, extracting the starting and ending index of numbers which is an increments of 8.
0-8
12-20
24-32
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.
Let me check!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2015년 7월 3일

댓글:

2015년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by