필터 지우기
필터 지우기

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

조회 수: 4 (최근 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 CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by