How to replace elements in a table using loops or any other method ?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608985/Variable%201.png)
![Variable 1](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608985/Variable%201.png)
Then I extracted it into 2 tables with each columns.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608990/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608995/image.png)
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 ?
0 Kommentare
Antworten (1)
KSSV
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!