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

2 views (last 30 days)
JIYON PJ on 6 May 2021
Answered: KSSV on 6 May 2021
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. 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 ?

KSSV on 6 May 2021
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!