Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Extracting the values from a cellarray

1 view (last 30 days)
Reshma Ravi
Reshma Ravi on 12 Jun 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a cellarray that is 'GUUA' [2] 'UUAG' [2] 'UAGC' [2] 'CGCC' [2] then my problem is to find the probability (individual item/total count, here p(GUUA)=2/8)of occurence. How is this possible.
Thanks in advance.

Answers (1)

Michael Byrne
Michael Byrne on 4 Aug 2017
Edited: Michael Byrne on 4 Aug 2017
One way to accomplish this is to use the contains function to get a logical array describing the locations of a specific string (in your example, 'GUUA'), and then summing this array to obtain the number of occurrences of this string. The number of occurrences can then be divided by the length of the cell array to obtain the probability:
c = {'GUUA','GUUA','UUAG','UUAG',...
'UAGC','UAGC','CGCC','CGCC'}; % Your cell array
p = sum(contains(c,'GUUA'))/length(c); % Probability
  1 Comment
Akira Agata
Akira Agata on 4 Aug 2017
Just a minor comment. If you want to count 'GUUA' exactly, I would recommend using strcmp, like:
p2 = sum(strcmp(c,'GUUA'))/length(c);

This question is closed.

Community Treasure Hunt

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

Start Hunting!