Issues converting a Cell to a numerical array
显示 更早的评论
Hi, I am trying to get the sum of all the prime unique nummbers in cell. I am having trouble converting the cell to a numerical array so that I can use the sum and prime functions on my inputs. The inputs are normally formated as ({1, 3, 7, 7, 'a'}) and matlab ether does not recognize these values as nummbers or can not convert a cell that has both nummbers and letters into a numerical array. TO be specific I am looking for a way to remove the letter from the cell and covert the rest of the cell to an array that I can use mathmatical operations on.
回答(1 个)
Here is one way that may work for you:
C = {1, 3, 7, 7, 'a'};
N = cell2mat(C(cellfun(@isnumeric,C)))
4 个评论
COLE ZESIGER
2021-2-25
the cyclist
2021-2-25
The documentation for primes indicates that it expects a scalar (not vector) input, and then reports all primes up to and including it.
isprime(N)
will tell you whether each element is prime.
COLE ZESIGER
2021-2-25
C = {1, 3, 7, 7, 'a'};
N = cell2mat(C(cellfun(@isnumeric,C)));
primeN = N(isprime(N))
lists only the prime numbers from C. You mentioned needing unique values somehow, so you might also need the unique function.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!