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.

回答(3 个)

% 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
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 个评论

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

请先登录,再进行评论。

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!

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by