check repeated values in a cell

7 visualizzazioni (ultimi 30 giorni)
NETHRAVATHI S
NETHRAVATHI S il 4 Giu 2021
Risposto: Rahul il 17 Ott 2024
Hello,
I have a cell with 90 elements in it.
in that, after 7th element all values are same.
I have to limit the repeatability for 3 times only. that is each set (3 times) of repeated values have to do some particular operation.
kindly help.
Code is too lenghty to attach here. this is my cell which i mentioned about.
24
1
1
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3

Risposte (1)

Rahul
Rahul il 17 Ott 2024
I understand that you wish to limit the count of repeating elemets in your cell array to a maximum of 3 and then do some operations on that cell array.
  • To achieve the desired result, you can use convert the cell array to a matrix using 'cell2mat' function.
  • Then you can use functions like 'unique' and 'accumarray' to find the unique elements of the array and their respective counts.
  • Then using a loop, all the elements can be appended to an array with the maximum count of the repeating elements to be 3 using the function 'repmat'. Here is an example:
% I have taken 'uniqueData' as the array obtained after using the 'unique' function
% I have taken 'counts' as the array obtained after using 'accumarray' function
output = {};
for i = 1:length(uniqueData)
element = uniqueData(i);
count = counts(i);
% Append limited elements to output
output = [output, repmat({element}, 1, min(count, 3))];
end
You can also refer to the following MathWorks documentations to knwo more about these functions:
Hope this helps! Thanks.

Categorie

Scopri di più su Functions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by