How to create a list of an array index
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 7 Nov. 2011
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 Kommentare
Image Analyst
am 7 Nov. 2011
Just do this
primesList = find(ary)+1;
The find() function tells you which elements are non-zero or true.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!