Overwrite table data with 'Yes' or 'No' according to the logical index

2 ビュー (過去 30 日間)
Smithy
Smithy 2022 年 9 月 1 日
コメント済み: Stephen23 2022 年 9 月 2 日
Hello everybody,
I would like to overwrite the table data if the logical index of row is true.
Logical index will be True if sampleinput.TEST_ITEM1 is'TV'
In case of True, the sampleinput.TEST_LOAD1 need to be changed with 'Yes'.
and False, the sampleinput.TEST_LOAD1 need to be changed with 'No'.
Please give me some help.
load sample1
idx = sampleinput.TEST_ITEM1 == "TV";
sampleinput.TEST_LOAD1(idx) = 'Yes';
idx = sampleinput.TEST_ITEM1 ~= "TV";
sampleinput.TEST_LOAD1(idx) = 'No';

採用された回答

Abderrahim. B
Abderrahim. B 2022 年 9 月 1 日
Hi!
Maybe this:
load sample1.mat
sampleinputarr = string(sampleinput.Variables) ;
tvIdx = sampleinputarr(:,1) == "TV" ;
sampleinputarr(tvIdx, 2) = "Yes" ;
sampleinputarr(~tvIdx, 2) = "No" ;
newTbl = array2table(sampleinputarr, "VariableNames", sampleinput.Properties.VariableNames )
newTbl = 11×2 table
TEST_ITEM1 TEST_LOAD1 __________ __________ "TV" "Yes" "TV" "Yes" "TV" "Yes" "TT" "No" "TV" "Yes" "TV" "Yes" "TV" "Yes" "TV" "Yes" "TL" "No" "TL" "No" "TL" "No"
Hope this helps
  2 件のコメント
Smithy
Smithy 2022 年 9 月 1 日
Thank you very mcuh. it works really well.
Abderrahim. B
Abderrahim. B 2022 年 9 月 1 日
My pleasure!

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

その他の回答 (1 件)

Stephen23
Stephen23 2022 年 9 月 1 日
編集済み: Stephen23 2022 年 9 月 1 日
Simpler:
S = load('sample1.mat');
T = S.sampleinput
T = 11×2 table
TEST_ITEM1 TEST_LOAD1 __________ __________ {'TV'} {'NaN'} {'TV'} {'NaN'} {'TV'} {'NaN'} {'TT'} {'NaN'} {'TV'} {'NaN'} {'TV'} {'NaN'} {'TV'} {'NaN'} {'TV'} {'NaN'} {'TL'} {'NaN'} {'TL'} {'NaN'} {'TL'} {'NaN'}
X = strcmpi(T.TEST_ITEM1,'TV');
T.TEST_LOAD1(:) = {'No'};
T.TEST_LOAD1(X) = {'Yes'}
T = 11×2 table
TEST_ITEM1 TEST_LOAD1 __________ __________ {'TV'} {'Yes'} {'TV'} {'Yes'} {'TV'} {'Yes'} {'TT'} {'No' } {'TV'} {'Yes'} {'TV'} {'Yes'} {'TV'} {'Yes'} {'TV'} {'Yes'} {'TL'} {'No' } {'TL'} {'No' } {'TL'} {'No' }
  2 件のコメント
Smithy
Smithy 2022 年 9 月 2 日
Thank you very much for your answer. It is really good
Stephen23
Stephen23 2022 年 9 月 2 日
@Smithy: remember that you can vote for my answer if you liked it!

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by