Remove rows in matrix A if not member of matrix B

4 ビュー (過去 30 日間)
Lulu Dulac
Lulu Dulac 2017 年 6 月 30 日
コメント済み: Lulu Dulac 2017 年 6 月 30 日
Hi all,
I have two matrices, and I am trying to remove rows in one for numbers which are not part of the other. Below is my attempt:
trials;
for x = 1:256
if ~ismember(trials,x);
rows2remove=[x];
imagesAll(rows2remove,:)=[];
end
% imagesCut =
end
But it is not working and I get the following error message (I guess because it keeps deleting rows?):
Matrix index is out of range for deletion.
Error in samplecode (line 21)
imagesAll(rows2remove,:)=[];
Please could anyone help me with my code? Many thanks

採用された回答

Stephen23
Stephen23 2017 年 6 月 30 日
編集済み: Stephen23 2017 年 6 月 30 日
MATLAB works best when working on entire arrays at once, whereas using a loop and removing rows like that is inefficient and not recommended.
>> tryIdx = [2,5,7,8];
>> allIdx = 1:size(imagesAll,1);
Method one: setdiff:
>> idx = setdiff(allIdx,tryIdx)
idx =
1 3 4 6 9
Method two: ismember:
>> idx = ~ismember(allIdx,tryIdx)
idx =
1 0 1 1 0 1 0 0 1
And then simply:
imagesAll(idx,:)
  3 件のコメント
Stephen23
Stephen23 2017 年 6 月 30 日
編集済み: Stephen23 2017 年 6 月 30 日
@Lulu Duvec: given your new description, it seems that simple indexing will do what you want:
imagesAll(trials,:)
Lulu Dulac
Lulu Dulac 2017 年 6 月 30 日
Thank you a lot!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by