How can I select the common value from table?

5 ビュー (過去 30 日間)
Mira le
Mira le 2021 年 9 月 15 日
回答済み: Amith 2024 年 10 月 9 日
Hello every one
I have this cell
S=
2
1 2 3
1 2 5
1 5
and the data like this
T=
1 2 4 5
2 3 5
1 2 4 5
1 2 3 5
each line from S I count it appearance in T as well as its index
I obtained this
vect=[ 3 1 3 4]
index=[1 2 3 4]
I sorted them and I obtained:
vect=[4 3 3 1 ]
index=[4 1 3 2]
So,
My issue is:
I want to remove the common value exist in almost all value of S
for example
value=2;
exist in all most of S, then I removed it
How Can I do thant?
thank you

回答 (1 件)

Amith
Amith 2024 年 10 月 9 日
Hi Mira,
To find the most frequently appearing element in all rows of your matrix, you can use the code below -
% Define the 3D matrix as a cell array to handle different row lengths
matrix = {[2], [1 2 3], [1 2 5], [1 5]};
% Concatenate all elements into a single array
allElements = [matrix{:}];
% Find the unique elements and their counts
[uniqueElements, ~, indices] = unique(allElements);
counts = accumarray(indices, 1);
% Find the element with the maximum count
[maxCount, maxIndex] = max(counts);
mostFrequentElement = uniqueElements(maxIndex);
% Display the result
fprintf('The most frequently occurring element is: %d\n', mostFrequentElement);
Hope this helps!

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by