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

2 vues (au cours des 30 derniers jours)
JIYON PJ
JIYON PJ le 6 Mai 2021
Réponse apportée : KSSV le 6 Mai 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.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 ?

Réponses (1)

KSSV
KSSV le 6 Mai 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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by