Changing values in atable

The data i want to change is in the 4th column.
I have tried using an IF statment but does not work:
for
if
table{:,4} >=5
disp '1'
else
disp '0'
end
Thank you in advance.

Respuestas (3)

Stephan
Stephan el 20 de Mzo. de 2021

0 votos

% Example table with random data
T = splitvars(table(randi(10,10,5)))
T =
10×5 table
Var1_1 Var1_2 Var1_3 Var1_4 Var1_5
______ ______ ______ ______ ______
9 7 2 10 7
3 5 2 2 8
7 7 1 2 6
6 6 5 7 4
6 7 5 1 2
9 6 4 6 6
3 8 8 6 3
4 6 7 9 1
2 10 8 5 8
10 3 10 4 3
% Some magic happens by logical indexing:
T{:,4} = T{:,4} >= 5
T =
10×5 table
Var1_1 Var1_2 Var1_3 Var1_4 Var1_5
______ ______ ______ ______ ______
9 7 2 1 7
3 5 2 0 8
7 7 1 0 6
6 6 5 1 4
6 7 5 0 2
9 6 4 1 6
3 8 8 1 3
4 6 7 1 1
2 10 8 1 8
10 3 10 0 3
Star Strider
Star Strider el 20 de Mzo. de 2021

0 votos

Try something like this:
T1 = array2table(randi(9, 10, 3))
Lv = T1.Var2 > 5;
T1.Var2(Lv,:) = 1;
T1.Var2(~Lv,:) = 0
producing:
T1 =
10×3 table
Var1 Var2 Var3
____ ____ ____
4 6 5
3 1 3
2 1 1
2 3 6
4 5 8
1 6 4
6 4 1
5 8 3
7 7 2
7 9 3
T1 =
10×3 table
Var1 Var2 Var3
____ ____ ____
4 1 5
3 0 3
2 0 1
2 0 6
4 0 8
1 1 4
6 0 1
5 1 3
7 1 2
7 1 3
.

4 comentarios

Mark Evans
Mark Evans el 20 de Mzo. de 2021
I have a csv file but when i use the table2array(T) to convert the table into an array it does not work?
Star Strider
Star Strider el 20 de Mzo. de 2021
Use the readtable function to import the .csv file into a table array.
Stephan
Stephan el 20 de Mzo. de 2021
Use readtable
Stephan
Stephan el 20 de Mzo. de 2021
Or if you want to work on an array replace the curly braces by normal braces

Iniciar sesión para comentar.

Sergio Yanez-Pagans
Sergio Yanez-Pagans el 20 de Mzo. de 2021
Editada: Sergio Yanez-Pagans el 20 de Mzo. de 2021

0 votos

A good way to get what you want is to convert your table to an array. Here is an example:
column1 = [2,3,4,5]'; % Data for table columns
T = table(column1); % Creating example column table
A = table2array(T) % Convert yout table to an aray for the moment
A > 5 % Will return array with 0s & 1s that satisfy the condition
Hope you find this useful!

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Mzo. de 2021

Editada:

el 23 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by