Prime number sum from cell array

3 views (last 30 days)
I'm currently trying to write a function that will input a cell array and will take an input such as getPrimeSum({1, 3, 7, 7, 'a'}) and return the sum of the prime numbers while ignoring characters and ignoring duplicates. The desired answer for that input example would be 10.
I'm relatively new to matlab and hung up on how to handle my input being inside the curly brackets because much of the code I am trying to use will not accept my input in that form. The second hangup is I'm not sure how to convert it since cell2mat() returns an error due to the character that is in the input. I thought I found a way around it with my first line which made the code usable, but my current code is returning 0 and I'm not sure why. Any advice is appreciated. My current progress is below.
function res = getPrimeSum(cArr);
cArr(cellfun(@ischar,cArr)) = {nan};
myArr = cell2mat(cArr);
filter1 = unique(myArr);
filter2 = isstrprop(myArr, 'digit');
onlyNumbers = myArr(filter2);
primeSum = sum(isprime(onlyNumbers));
res = primeSum;
end

Accepted Answer

Simon Allosserie
Simon Allosserie on 1 Mar 2021
There are a few issues.
  • I wouldn't change characters to NaN but rather to an empty cell.
  • After your cellfun and filter1, myArr only contains numbers (assuming your input only contains numbers and chars). filter2 shows what parts of a string are digits. As after filter1, your myArr only contains numbers, there are no strings and thus also no digits in a string. Consequently, filter2 is a zero matrix, and onlyNumbers will be empty. If you can't get strings ("...") as an input, and only chars ('...'), then you can just delete the filter2 part, because after applying filter1, myArr only contains numbers.
  • functions like isprime are binary and just show 0's or 1's where the condition (in this case, 'is this number a prime?') is or isn't met. isprime(OnlyNumbers) is thus something of the form [0 1 1]. If you take the sum of this, you sum up the 1's instead of the values that correspond to a one. Consequently, you must apply this mask to your myArr to sum up the values itself.
Adapting these issues makes your code like this
function res = getPrimeSum(cArr);
cArr(cellfun(@ischar,cArr)) = {[]}; %this makes an empty double, avoids problems furtheron with NaN
myArr = cell2mat(cArr); %change cell values to doubles
myArr = unique(myArr); %select the unique elements only
res = sum(myArr(isprime(myArr))); %sum up the values that are primes
end

More Answers (1)

Jan
Jan on 1 Mar 2021
Edited: Jan on 1 Mar 2021
The variable myArr is a double array. Then isstrprop(myArr, 'digit') is FALSE for all elements, because onbly CHARs can be digits. Simply omit this test:
function X = getPrimeSum(C)
C(cellfun(@ischar, C)) = {nan}; % Better: C(cellfun(@ischar, C)) = []
X = unique(cell2mat(C));
S = sum(isprime(X));
end
Use the debugger to examine such problems: Set a breakpoint in the first line of the code and step through your function line by line.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!