Extract certain rows from matrix

조회 수: 2 (최근 30일)
Nathan Paul
Nathan Paul 2016년 5월 6일
댓글: Guillaume 2016년 5월 6일
I have a data matrix with 4 columns and 'n' number of rows, I want to extract the rows where the first two columns match certain values from the first two columns.
E.g where they match every possible combination of column 1 = 0.05, 0.1, 0.2, 0.5 and column 2 = 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 3.0
I am extremely stuck I've managed to find what every possible combination would be for column 1 and 2 but dont know how to apply that to the data matrix
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 6일
You posted an example of two columns with different sizes.
Guillaume
Guillaume 2016년 5월 6일
The set of valid values for column 1 does not have to be the same size as the set of valid values for column 2. I don't see any issue with that.

댓글을 달려면 로그인하십시오.

답변 (2개)

KSSV
KSSV 2016년 5월 6일
You can subtract the second column with first column. Where ever zero is there, extract that row.

Guillaume
Guillaume 2016년 5월 6일
To test if members of a set belong to another set you use ismember, with the 'rows' option in your case:
%given:
col1set = [0.05, 0.1, 0.2, 0.5];
col2set = [0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 3.0];
%demo data
data = [0.05, 0.5, 1, 1;
0.3, 0.05, 2, 2;
0.2, 0.4, 3, 3;
0.1, 0.1, 4, 4;
0.5, 0.05, 5, 5;
0.05, 0.5, 6, 6]; %demo data
%build every combinations of col1set with col2set
%I'm using ndgrid for this. There are other ways
[c1, c2] = ndgrid(col1set, col2set);
validrows = [c1(:), c2(:)];
%find which rows of data match validrows, only for column 1 and 2:
tokeep = ismember(data(:, [1 2]), validrows, 'rows');
%and use that to filter the original array
filtereddata = data(tokeep, :)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by