Delete row and column from matrix

2 ビュー (過去 30 日間)
Rachel Ramirez
Rachel Ramirez 2020 年 12 月 9 日
回答済み: Kedar Ingle 2020 年 12 月 9 日
I have matrix A that is 100x5 and a matrix B that is 10x2. I want to go through matrix A and delete all rows that have matrix B first column values in matrix A but only from it's first and/or second column (not the other 3). I'm thinking of a foor loop and and if statement inside but not sure how to. Could you someone elaborate how to approach the problem?
  1 件のコメント
KSSV
KSSV 2020 年 12 月 9 日
Read about ismember. This will give you indices which are common in both the matrices.

サインインしてコメントする。

回答 (1 件)

Kedar Ingle
Kedar Ingle 2020 年 12 月 9 日
It is my understanding that you want to remove rows from matrix A whose elements in column 1 or 2 come from column 1 of matrix B?
The checking of whether matrix A elements (from columns 1 and 2) are present in matrix B (column 1) could be done with the function ismember. The solution could look something like this:
function [output] = reduceA(A,B)
i=1;
while i <= size(A, 1) % while not for, as the loop size keeps on reducing
if(ismember(A(i,1), B(:, 1)) || ismember(A(i,2), B(:, 1)))
A(i, :) = [];
else % only increment the counter if we haven't deleted a row...
i = i + 1;
end
end
output = A;
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by