conversion of yes no into logical array

조회 수: 7 (최근 30일)
uma
uma 2022년 5월 10일
댓글: Stephen23 2022년 6월 5일
I have attached my dataset file "gridstability", I want to covert my last coulmn "stabf" into logical values 0 and 1. i want to know how to perform this operation.

채택된 답변

Davide Masiello
Davide Masiello 2022년 5월 10일
편집: Davide Masiello 2022년 5월 10일
clear,clc
T = readtable('gridstability.csv')
T = 10000×14 table
tau1 tau2 tau3 tau4 p1 p2 p3 p4 g1 g2 g3 g4 stab stabf _______ ______ _______ _______ ______ ________ ________ ________ _______ _______ _______ _______ __________ ____________ 2.9591 3.0799 8.381 9.7808 3.7631 -0.7826 -1.2574 -1.7231 0.65046 0.85958 0.88744 0.95803 0.055347 {'unstable'} 9.3041 4.9025 3.0475 1.3694 5.0678 -1.9401 -1.8727 -1.255 0.41344 0.86241 0.56214 0.78176 -0.0059575 {'stable' } 8.9717 8.8484 3.0465 1.2145 3.4052 -1.2075 -1.2772 -0.92049 0.16304 0.76669 0.83944 0.10985 0.0034709 {'unstable'} 0.71641 7.6696 4.4866 2.3406 3.9638 -1.0275 -1.9389 -0.99737 0.44621 0.97674 0.92938 0.36272 0.028871 {'unstable'} 3.1341 7.6088 4.9438 9.8576 3.5258 -1.1255 -1.846 -0.55431 0.79711 0.45545 0.65695 0.82092 0.04986 {'unstable'} 6.9992 9.1092 3.7841 4.2678 4.4297 -1.8571 -0.6704 -1.9021 0.26179 0.07793 0.54288 0.46993 -0.017385 {'stable' } 6.7102 3.7652 6.9293 8.8186 2.3974 -0.61459 -1.2088 -0.574 0.17789 0.39798 0.40205 0.37663 0.0059536 {'unstable'} 6.9535 1.3791 5.7194 7.8703 3.2245 -0.749 -1.1865 -1.289 0.37138 0.6332 0.73274 0.38054 0.016634 {'unstable'} 4.6899 4.0077 1.4786 3.7338 4.0413 -1.4103 -1.2382 -1.3928 0.26971 0.25036 0.16494 0.48244 -0.038677 {'stable' } 9.8415 1.4138 9.7699 7.6416 4.7276 -1.9914 -0.85764 -1.8786 0.37636 0.54442 0.79204 0.11626 0.012383 {'unstable'} 5.9301 6.7309 6.2451 0.53329 2.3271 -0.7025 -1.1169 -0.50767 0.23982 0.56311 0.16446 0.7537 -0.028411 {'stable' } 5.3813 8.0145 8.0952 6.7692 5.5076 -1.9727 -1.8493 -1.6855 0.35997 0.17357 0.34914 0.62886 0.02813 {'unstable'} 1.6168 2.9392 0.81979 4.1918 3.7523 -1.4849 -1.2806 -0.98682 0.8997 0.86655 0.30392 0.07761 -0.048617 {'stable' } 8.5516 8.315 2.55 9.9268 4.8917 -1.8086 -1.1671 -1.916 0.6124 0.28098 0.35434 0.47219 0.027756 {'unstable'} 1.1321 2.9203 8.9511 7.2486 5.0337 -1.8461 -1.3628 -1.8248 0.35229 0.52417 0.599 0.67439 0.01488 {'unstable'} 7.0214 4.3743 4.7759 8.8384 3.3359 -0.96239 -1.4076 -0.96584 0.7111 0.62536 0.46833 0.89514 0.072508 {'unstable'}
If you are ok with T.stabf being populated by true/false values, you can use.
T.stabf = strcmp(T.stabf,{'stable'});
T
T = 10000×14 table
tau1 tau2 tau3 tau4 p1 p2 p3 p4 g1 g2 g3 g4 stab stabf _______ ______ _______ _______ ______ ________ ________ ________ _______ _______ _______ _______ __________ _____ 2.9591 3.0799 8.381 9.7808 3.7631 -0.7826 -1.2574 -1.7231 0.65046 0.85958 0.88744 0.95803 0.055347 false 9.3041 4.9025 3.0475 1.3694 5.0678 -1.9401 -1.8727 -1.255 0.41344 0.86241 0.56214 0.78176 -0.0059575 true 8.9717 8.8484 3.0465 1.2145 3.4052 -1.2075 -1.2772 -0.92049 0.16304 0.76669 0.83944 0.10985 0.0034709 false 0.71641 7.6696 4.4866 2.3406 3.9638 -1.0275 -1.9389 -0.99737 0.44621 0.97674 0.92938 0.36272 0.028871 false 3.1341 7.6088 4.9438 9.8576 3.5258 -1.1255 -1.846 -0.55431 0.79711 0.45545 0.65695 0.82092 0.04986 false 6.9992 9.1092 3.7841 4.2678 4.4297 -1.8571 -0.6704 -1.9021 0.26179 0.07793 0.54288 0.46993 -0.017385 true 6.7102 3.7652 6.9293 8.8186 2.3974 -0.61459 -1.2088 -0.574 0.17789 0.39798 0.40205 0.37663 0.0059536 false 6.9535 1.3791 5.7194 7.8703 3.2245 -0.749 -1.1865 -1.289 0.37138 0.6332 0.73274 0.38054 0.016634 false 4.6899 4.0077 1.4786 3.7338 4.0413 -1.4103 -1.2382 -1.3928 0.26971 0.25036 0.16494 0.48244 -0.038677 true 9.8415 1.4138 9.7699 7.6416 4.7276 -1.9914 -0.85764 -1.8786 0.37636 0.54442 0.79204 0.11626 0.012383 false 5.9301 6.7309 6.2451 0.53329 2.3271 -0.7025 -1.1169 -0.50767 0.23982 0.56311 0.16446 0.7537 -0.028411 true 5.3813 8.0145 8.0952 6.7692 5.5076 -1.9727 -1.8493 -1.6855 0.35997 0.17357 0.34914 0.62886 0.02813 false 1.6168 2.9392 0.81979 4.1918 3.7523 -1.4849 -1.2806 -0.98682 0.8997 0.86655 0.30392 0.07761 -0.048617 true 8.5516 8.315 2.55 9.9268 4.8917 -1.8086 -1.1671 -1.916 0.6124 0.28098 0.35434 0.47219 0.027756 false 1.1321 2.9203 8.9511 7.2486 5.0337 -1.8461 -1.3628 -1.8248 0.35229 0.52417 0.599 0.67439 0.01488 false 7.0214 4.3743 4.7759 8.8384 3.3359 -0.96239 -1.4076 -0.96584 0.7111 0.62536 0.46833 0.89514 0.072508 false
However, if you want 0s and 1s strictly, you can use
clear,clc
T = readtable('gridstability.csv')
T = 10000×14 table
tau1 tau2 tau3 tau4 p1 p2 p3 p4 g1 g2 g3 g4 stab stabf _______ ______ _______ _______ ______ ________ ________ ________ _______ _______ _______ _______ __________ ____________ 2.9591 3.0799 8.381 9.7808 3.7631 -0.7826 -1.2574 -1.7231 0.65046 0.85958 0.88744 0.95803 0.055347 {'unstable'} 9.3041 4.9025 3.0475 1.3694 5.0678 -1.9401 -1.8727 -1.255 0.41344 0.86241 0.56214 0.78176 -0.0059575 {'stable' } 8.9717 8.8484 3.0465 1.2145 3.4052 -1.2075 -1.2772 -0.92049 0.16304 0.76669 0.83944 0.10985 0.0034709 {'unstable'} 0.71641 7.6696 4.4866 2.3406 3.9638 -1.0275 -1.9389 -0.99737 0.44621 0.97674 0.92938 0.36272 0.028871 {'unstable'} 3.1341 7.6088 4.9438 9.8576 3.5258 -1.1255 -1.846 -0.55431 0.79711 0.45545 0.65695 0.82092 0.04986 {'unstable'} 6.9992 9.1092 3.7841 4.2678 4.4297 -1.8571 -0.6704 -1.9021 0.26179 0.07793 0.54288 0.46993 -0.017385 {'stable' } 6.7102 3.7652 6.9293 8.8186 2.3974 -0.61459 -1.2088 -0.574 0.17789 0.39798 0.40205 0.37663 0.0059536 {'unstable'} 6.9535 1.3791 5.7194 7.8703 3.2245 -0.749 -1.1865 -1.289 0.37138 0.6332 0.73274 0.38054 0.016634 {'unstable'} 4.6899 4.0077 1.4786 3.7338 4.0413 -1.4103 -1.2382 -1.3928 0.26971 0.25036 0.16494 0.48244 -0.038677 {'stable' } 9.8415 1.4138 9.7699 7.6416 4.7276 -1.9914 -0.85764 -1.8786 0.37636 0.54442 0.79204 0.11626 0.012383 {'unstable'} 5.9301 6.7309 6.2451 0.53329 2.3271 -0.7025 -1.1169 -0.50767 0.23982 0.56311 0.16446 0.7537 -0.028411 {'stable' } 5.3813 8.0145 8.0952 6.7692 5.5076 -1.9727 -1.8493 -1.6855 0.35997 0.17357 0.34914 0.62886 0.02813 {'unstable'} 1.6168 2.9392 0.81979 4.1918 3.7523 -1.4849 -1.2806 -0.98682 0.8997 0.86655 0.30392 0.07761 -0.048617 {'stable' } 8.5516 8.315 2.55 9.9268 4.8917 -1.8086 -1.1671 -1.916 0.6124 0.28098 0.35434 0.47219 0.027756 {'unstable'} 1.1321 2.9203 8.9511 7.2486 5.0337 -1.8461 -1.3628 -1.8248 0.35229 0.52417 0.599 0.67439 0.01488 {'unstable'} 7.0214 4.3743 4.7759 8.8384 3.3359 -0.96239 -1.4076 -0.96584 0.7111 0.62536 0.46833 0.89514 0.072508 {'unstable'}
T.stabf = double(strcmp(T.stabf,{'stable'}));
T
T = 10000×14 table
tau1 tau2 tau3 tau4 p1 p2 p3 p4 g1 g2 g3 g4 stab stabf _______ ______ _______ _______ ______ ________ ________ ________ _______ _______ _______ _______ __________ _____ 2.9591 3.0799 8.381 9.7808 3.7631 -0.7826 -1.2574 -1.7231 0.65046 0.85958 0.88744 0.95803 0.055347 0 9.3041 4.9025 3.0475 1.3694 5.0678 -1.9401 -1.8727 -1.255 0.41344 0.86241 0.56214 0.78176 -0.0059575 1 8.9717 8.8484 3.0465 1.2145 3.4052 -1.2075 -1.2772 -0.92049 0.16304 0.76669 0.83944 0.10985 0.0034709 0 0.71641 7.6696 4.4866 2.3406 3.9638 -1.0275 -1.9389 -0.99737 0.44621 0.97674 0.92938 0.36272 0.028871 0 3.1341 7.6088 4.9438 9.8576 3.5258 -1.1255 -1.846 -0.55431 0.79711 0.45545 0.65695 0.82092 0.04986 0 6.9992 9.1092 3.7841 4.2678 4.4297 -1.8571 -0.6704 -1.9021 0.26179 0.07793 0.54288 0.46993 -0.017385 1 6.7102 3.7652 6.9293 8.8186 2.3974 -0.61459 -1.2088 -0.574 0.17789 0.39798 0.40205 0.37663 0.0059536 0 6.9535 1.3791 5.7194 7.8703 3.2245 -0.749 -1.1865 -1.289 0.37138 0.6332 0.73274 0.38054 0.016634 0 4.6899 4.0077 1.4786 3.7338 4.0413 -1.4103 -1.2382 -1.3928 0.26971 0.25036 0.16494 0.48244 -0.038677 1 9.8415 1.4138 9.7699 7.6416 4.7276 -1.9914 -0.85764 -1.8786 0.37636 0.54442 0.79204 0.11626 0.012383 0 5.9301 6.7309 6.2451 0.53329 2.3271 -0.7025 -1.1169 -0.50767 0.23982 0.56311 0.16446 0.7537 -0.028411 1 5.3813 8.0145 8.0952 6.7692 5.5076 -1.9727 -1.8493 -1.6855 0.35997 0.17357 0.34914 0.62886 0.02813 0 1.6168 2.9392 0.81979 4.1918 3.7523 -1.4849 -1.2806 -0.98682 0.8997 0.86655 0.30392 0.07761 -0.048617 1 8.5516 8.315 2.55 9.9268 4.8917 -1.8086 -1.1671 -1.916 0.6124 0.28098 0.35434 0.47219 0.027756 0 1.1321 2.9203 8.9511 7.2486 5.0337 -1.8461 -1.3628 -1.8248 0.35229 0.52417 0.599 0.67439 0.01488 0 7.0214 4.3743 4.7759 8.8384 3.3359 -0.96239 -1.4076 -0.96584 0.7111 0.62536 0.46833 0.89514 0.072508 0
The code above outputs a logical 1 (true) if the row is 'stable' and a logical 0 (false) is the row is 'unstable'.
For the opposite result, just insert a ~ before the strcmp command.
  댓글 수: 2
Stephen23
Stephen23 2022년 6월 5일
uma's incorrectly posted "Answers" moved here:
how can i get this entire table with last column replaced by 0 and 1.
what if i have to convert it 1 and 2 rather than in logical?
Stephen23
Stephen23 2022년 6월 5일
@uma: add 1

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by