check repeated values in a cell

조회 수: 7 (최근 30일)
NETHRAVATHI S
NETHRAVATHI S 2021년 6월 4일
답변: Rahul 2024년 10월 17일
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

답변 (1개)

Rahul
Rahul 2024년 10월 17일
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.

카테고리

Help CenterFile Exchange에서 Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by