How do i check the frequency of an element in a multi level cell array?

The multi level cell array has 61 cells and each of these 61 elements have around 25*30 cells.

7 Kommentare

Are the lowest-level cells all the same size?
Dinesh Yadav
Dinesh Yadav am 23 Jan. 2020
Bearbeitet: Dinesh Yadav am 23 Jan. 2020
Adding upon Sindar's question are datatypes of all elements same or they can be different? Can you share how your data looks like?
>> result2
result2 =
1×25 cell array
Columns 1 through 6
{25×1 cell} {24×1 cell} {26×1 cell} {26×1 cell} {23×1 cell} {26×1 cell}
Columns 7 through 12
{26×1 cell} {26×1 cell} {15×1 cell} {24×1 cell} {26×1 cell} {26×1 cell}
Columns 13 through 18
{26×1 cell} {23×1 cell} {13×1 cell} {27×1 cell} {27×1 cell} {24×1 cell}
Columns 19 through 24
{24×1 cell} {23×1 cell} {26×1 cell} {26×1 cell} {21×1 cell} {26×1 cell}
Column 25
{23×1 cell}
>> result2{1,1}
ans =
25×1 cell array
{["LT4437"]}
{["PP5041"]}
{["TP2019"]}
{["RP3015"]}
{["RP3012"]}
{["RP2018"]}
{1×2 string}
{["RP2010"]}
{["RP2126"]}
{["RP1030"]}
{["RP1015"]}
{["RP2040"]}
{["RP1050"]}
{["RP2020"]}
{["RP2030"]}
{["RP2050"]}
{["RP1050"]}
{["RP4008"]}
{["RP1025"]}
{["RP3012"]}
{["RP3010"]}
{["RP3020"]}
{["RP3020"]}
{["RP3015"]}
{["RP3010"]}
I want to check how many times each element appears in the array
Do you want to compare a given string with all strings in results2 or just the given first level cell? What do you want to do with that info?
I want to compare each element with every other element in the entire array(multi level) and output a similar structure array with the frequency instead of the element name
Is there a reason you need a new array with similar structure to results2? It'd probably be much easier to make a unique list of the strings and list the corresponding frequencies alongside them.
Secondly, it's worth confirming what Dinesh asked with the following code:
test = cellfun(@(x) cellfun(@class,x,'UniformOutput',false),results2,'UniformOutput',false);
unique(vertcat(test{:}))
Or better yet, attach results2, with the paperclip button.
I just need to know which elements in this array only appear once .
and yes i agree that a unique list of strings and corresponding frequencies should be fine given that they are in the same order as they are present in the arrays.
I am attaching results 2 variable
Thanks a lot!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Turlough Hughes
Turlough Hughes am 23 Jan. 2020
Bearbeitet: Turlough Hughes am 24 Jan. 2020
The following loads and concatenates all elements of your cell array, including the 1 x 2 string in result2{1,1}(7), into a single column string array called elements:
load asd
elements = vertcat(result2{:});
elements = [elements{:}].';
The next step is to get a unique list of names and the index iNames which can be used to get frequency as follows:
[NameList,~,iNames] = unique(elements,'stable');
Frequency = histcounts(iNames,numel(NameList)).';
T = table(NameList, Frequency)
The second input to the unique function is used to maintain the order of the input array, you might also like to have a look at the documentation on unique regarding the third output.
Edit: Modified unique to maintain the order and included some description of the code.

2 Kommentare

Great!
But the list is in chronological order , I need it in the order exactly as it is in the array.
I modified my answer slightly; just including 'stable' on the second input to the unique function. The table, T, then has the results ordered according to how they first appear in result2, moving through your original cell array as follows: result2{1,1}, result2{1,2}, result2{1,3}, etc. I assume that's what you mean.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by