简体   繁体   中英

Remove columns from matrix by logical array: Matlab

I have a matrix in Matlab and I want to remove some columns from it. I have also a vector with the indices that I want to remove. How exactly I can do so?

train_data   % My input matrix with size 1500x773
toremove     % 1x773 logical vector values (0,1), 1 at 40 indices

How can I apply toremove to the train_data to remove the desired indices?

output = train_data(toremove) % I want the output  to be a matrix with size 1500x733

If your array is truly logical (true/false) you can use it directly for indexing, it sounds like it's binary though (0/1), so you can use logical(toremove) to convert it to logical, then it's simple:

train_data = train_data(:,~logical(toremove));
% or equivalently
train_data(:, logical(toremove)) = [];

Avoiding a call to the find function will increase speed.

If you wish to delete columns in 2d matrix based off 1d column matrix:

output = train_data(:,find(toremove<1));

If it's rows that need deleting instead, based off 1d row matrix:

output = train_data(find(toremove<1),:);

Might do the job if I understand this correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM