How do you remove a value from a table based on its value

I am trying to remove the columns from the tollowing Combined table if they have a Value of 1.
I am a trying to find a method that deletes the colunn based on its value rather than deleting the column based on its heading, as it value may change to a 1 or 0 depending on the data set.
This is what I currently have but I am recieving the following error. If anyone has any advice that would be great!
Value = ([0,0,1,1,0])
Value = 1×5
0 0 1 1 0
Heading = (["Radius", "Speed", "Type", "Location", "ID"])
Heading = 1×5 string array
"Radius" "Speed" "Type" "Location" "ID"
Combined = array2table([Value; Heading])
Combined = 2×5 table
Var1 Var2 Var3 Var4 Var5 ________ _______ ______ __________ ____ "0" "0" "1" "1" "0" "Radius" "Speed" "Type" "Location" "ID"
Combined(Combined(1,:)>0,:) = []
Error using >
Applying the function 'gt' to the variable 'Var1' generated an error.

Caused by:
Error using >
Comparison between string and double is not supported.

2 件のコメント

VBBV
VBBV 2023 年 5 月 8 日
% first approach & simpler
Value = ([0,0,1,1,0]);
idx = Value > 0;
Heading = (["Radius", "Speed", "Type", "Location", "ID"]);
Combined = array2table([Value; Heading]);
Combined(:,idx) = []
Combined = 2×3 table
Var1 Var2 Var5 ________ _______ ____ "0" "0" "0" "Radius" "Speed" "ID"
% second approach using additional functions
Value = ([0,0,1,1,0]);
Heading = (["Radius", "Speed", "Type", "Location", "ID"]);
Combined = array2table([Value; Heading]);
idx = str2double(table2array(Combined(1,:))) > 0
idx = 1×5 logical array
0 0 1 1 0
Combined(:,idx) = []
Combined = 2×3 table
Var1 Var2 Var5 ________ _______ ____ "0" "0" "0" "Radius" "Speed" "ID"
Daniel Gaggini
Daniel Gaggini 2023 年 5 月 8 日
Thank you very much for your time and your answer.

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

 採用された回答

the cyclist
the cyclist 2023 年 5 月 7 日
編集済み: the cyclist 2023 年 5 月 7 日

0 投票

One can do what you ask as follows:
Value = ([0,0,1,1,0]);
Heading = (["Radius", "Speed", "Type", "Location", "ID"]);
Combined = array2table([Value; Heading]);
Combined(:,Combined{1,:} == "1") = []
Combined = 2×3 table
Var1 Var2 Var5 ________ _______ ____ "0" "0" "0" "Radius" "Speed" "ID"
In your simple example, MATLAB had to convert the numeric values 0 and 1 to the strings "0" and "1", to store them in the same column with other strings.

2 件のコメント

the cyclist
the cyclist 2023 年 5 月 7 日
Also, I can't shake the feeling that you are storing your data in a table in a very unconventional way, and therefore will experience many of these awkward operations. I suggest learning about the tidy format.
Daniel Gaggini
Daniel Gaggini 2023 年 5 月 8 日
Thank you very much for your time and your answer. Also thanks for the learning link. :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by