MATLAB Answers

How to replace elements in a table using loops or any other method ?

2 ビュー (過去 30 日間)
JIYON PJ
JIYON PJ 2021 年 5 月 6 日
回答済み: KSSV 2021 年 5 月 6 日
I am trying as an exercise to replace all elements in a table and I cannot figure it out. I am fairly new to Matlab and basic programming skills are not helping on this. Maybe thngs work different here.
So I have created an excel file with 23 rows and 2 columns with first column containing either 'Y' or 'N' for yes and no & the other with 2 digit number. I imported it as table
as shown.Variable 1
Then I extracted it into 2 tables with each columns.
I want to convert the table with 'Y' and 'N' to have either 1 or 0 based on Y & N. But using loop is not getting me anywhere. What am I doing wrong / What is the correct method to do this ?

回答 (1 件)

KSSV
KSSV 2021 年 5 月 6 日
C1 = {'Y' 'Y' 'N' 'Y' 'N'}' ;
C2 = rand(size(C1)) ;
T = table(C1,C2) ;
% replace Y a 1 and N as 0
C = zeros(size(C1)) ;
idx = strcmp(T.(1),'Y') ;
C(idx) = 1 ;
T.C3 = C

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by