Deleting several specific columns from a table

I would like to delete several hundred columns from a table of data. I'm currently using the commands:
QuestionnairesData(:,4:5) = []
QuestionnairesData(:,5:6) = []
QuestionnairesData(:,6:7) = []
But this goes on for several hundred lines which would take hours to run. Does anyone know a more efficient way to do this? Thank you!

 採用された回答

the cyclist
the cyclist 2019 年 8 月 29 日

1 投票

Do you know all the column indices you want to delete? It's as simple as
columnIndicesToDelete = [4 5 6 7 9 12 14 15 17]; % and so on
QuestionnairesData(:,columnIndicesToDelete) = [];
And if there is some pattern to the indices, you won't necessarily have to specify every single value.
Or am I missing something? In your example, the columns deleted in the command
QuestionnairesData(:,5:6) = [];
after having done
QuestionnairesData(:,4:5) = [];
will not be the 5th and 6th column of the original array. So maybe there is some complication there?

3 件のコメント

Darla Bonagura
Darla Bonagura 2019 年 8 月 29 日
I just tested out the first command you provided and it worked beautifully. Thank you! Do you think if I enter 300+ values into the brackets that it could cause MATLAB to crash?
The reason I set it up as described is because after I deleted columns 5 and 6 (100x10), 4 and 5 become the next columns I want to delete (100x8). As a MATLAB beginner, I'm learning that wasn't the most efficient way to do it.
Thank you again!
Jos (10584)
Jos (10584) 2019 年 8 月 29 日
No it will not crash!
T = array2table(randi(100,2,100000)) ; % a table with 100000 columns!
idx = 2:99999 ;
T(:,idx) = [] % delete 99998 columns
Alexander Carmeli
Alexander Carmeli 2021 年 6 月 30 日
This line will conveniently remove every column that has all-zero elements:
t(:,~any(t{:,:})) = []

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by