필터 지우기
필터 지우기

Delete columns of a matrix based on values in other matrix

조회 수: 1 (최근 30일)
Conor Nelson
Conor Nelson 2019년 7월 22일
댓글: Adam Danz 2019년 7월 22일
I have 3 matrices. The 2nd column of Matrix #1 counts up from 1 to 200. I want to check the 4th column of matrix #2 and the 10th column of matrix #3. If Matrix #1 contains a value in col2 that matrix 2 col4 or matrix 3 col10 does not have, i want to delete or turn the whole row containing that uncommon values to zeros.
Ex.
Matrix 1 (col2) Matrix 2 (col4) Matrix 3 (col10)
1 2 1
2 3 2
3 4 3
4 5 4
5 7 5
6 8 7
7 9 9
8 10 10
9 11 11
10 12 12
...
In this case, i am looking for rows 1, 6 and 8 to be deleted(or turned to zeros) from Matrix 1. Row 6 of Matrix 2 and row 1 of matrix 3.
Thanks

채택된 답변

Adam Danz
Adam Danz 2019년 7월 22일
편집: Adam Danz 2019년 7월 22일
Replaces identified rows with zeros.
% Example data that match the indices in your question
M1 = rand(10,10); M1(:,2) = 1:10;
M2 = rand(10,10); M2(:,4) = [2:5,7:12];
M3 = rand(10,10); M3(:,10) = [1:5,7,9:12];
% Identify rows that should be eliminated; replace data with zeros.
idx = ~ismember(M1(:,2),M2(:,4)) | ~ismember(M1(:,2),M3(:,10));
M1(idx,:) = 0;
Note that row 6 should also be altered in addition to row 1 and 8.
If you'd rather delete those rows in M1 rather than replace the values with 0s,
M1(idx,:) = [];
  댓글 수: 4
Conor Nelson
Conor Nelson 2019년 7월 22일
편집: Conor Nelson 2019년 7월 22일
Doesnt seem to be working correctly for my application of the code (more complex example than the one i supplied above), i will continue to mess around with it
Adam Danz
Adam Danz 2019년 7월 22일
If you attach a mat file with M1, M2, M3 matrices, I can chip in.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by