Changing values in atable
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (3개)
Stephan
2021년 3월 20일
% 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
댓글 수: 0
Star Strider
2021년 3월 20일
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
Sergio Yanez-Pagans
2021년 3월 20일
편집: Sergio Yanez-Pagans
2021년 3월 20일
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!