How to create a list of an array index

조회 수: 20 (최근 30일)
Scragmore
Scragmore 2011년 11월 6일
I am doing a project Euler prime number puzzle but I do not wish to use the Matlab primes function , so I'm building my own.
I am going to use the standard sieve to produce my primes but I wish to store these in a logic array. How do I get the index/cell location of all true values into an array/output as these will be my prime numbers.
Thanks
AD

채택된 답변

Image Analyst
Image Analyst 2011년 11월 7일
Can't you just use a counter that increments when you find a prime number?
if itsAPrimeNumber
numberOfPrimesFound = numberOfPrimesFound + 1;
listOfPrimes(numberOfPrimesFound) = yourCurrentNumber;
end
  댓글 수: 2
Scragmore
Scragmore 2011년 11월 7일
If possible I would like to do it this way, learning to do bitwise calculations, bigger array/smaller memory allocation, how I want to use the function ect.
so if I have an array;
ary = [1 1 0 1 0 1 0 0 0 1 .....]
I would like to return;
return ary(true)
ans = 2 3 5 7 11 .......
Image Analyst
Image Analyst 2011년 11월 7일
Just do this
primesList = find(ary)+1;
The find() function tells you which elements are non-zero or true.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by