Changing values in atable

8 views (last 30 days)
Mark Evans
Mark Evans on 20 Mar 2021
Edited: Mark Evans on 23 Mar 2021
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.

Answers (3)

Stephan
Stephan on 20 Mar 2021
% 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 on 20 Mar 2021
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 Comments
Stephan
Stephan on 20 Mar 2021
Use readtable
Stephan
Stephan on 20 Mar 2021
Or if you want to work on an array replace the curly braces by normal braces

Sign in to comment.


Sergio Yanez-Pagans
Sergio Yanez-Pagans on 20 Mar 2021
Edited: Sergio Yanez-Pagans on 20 Mar 2021
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!

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!