delete positions from a matrix according to a given set of conditions based on matrix elements

1 回表示 (過去 30 日間)
Hello, what I intend to achieve starting from an example matrix like the one that could be data:
Detect for each different layer (variable in the fourth column) the element that is most repeated from the third one.
And finally delete all the columns of those elements that in that layer do not have the value of the one that is most repeated.
Notes.
- The data shown in the images in columns VAL1 and VAL2 do not match with the ones I attached but it is not important.
- When I say delete I delete those rows, the only thing that for the example in the figure I have left them as blanks just to try to explain myself better.
I have a code that I thought to do it but in the real case the matrices are very long and those loops are a bit "crappy", can it be done in a more efficient way?
clear;
load data;
M = zeros(data(end,4),1);
for k = 0 : data(end,4)
M(k+1) = mode(data(data(:,4)==k,3));
end
for k = 1 : size(data,1)
if data(k,3) ~= M(data(k,4)+1)
data(k,:) = NaN(1,size(data,2));
end
end
data(~any(~isnan(data), 2),:) = [];

採用された回答

Ive J
Ive J 2021 年 2 月 2 日
編集済み: Ive J 2021 年 2 月 2 日
One possible solution would be:
tab = array2table(data);
tab1 = groupsummary(tab, {'data3', 'data4'}) % count VAL3 per each layer
tab1 = groupfilter(tab1, 'data4' ,@(x) x == max(x), 'GroupCount');% keep only those with max count per each layer
tab(~ismember(tab.data3, tab1.data3), :) = []; % remove other rows
tab
15×4 table
data1 data2 data3 data4
_____ _____ _____ _____
0.09 0.84 1 0
0.28 0.76 1 0
0.36 0.69 1 0
0.04 0.95 1 0
0.47 0.11 1 0
0.8 0.36 1 0
0.68 0.66 2 1
0.44 0.12 2 1
0.39 0.58 2 1
0.79 0.81 2 1
0.2 0.07 2.6 2
0.59 0.71 2.6 2
0.27 0.96 2.6 2
0.08 0.95 2.6 2
0.34 0.57 2.6 2
newData = tab{:, :}; % convert back to array

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSignal Operations についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by