divide every numeric value in table by same number

조회 수: 51 (최근 30일)
Davindra Usov
Davindra Usov 2023년 3월 26일
댓글: Walter Roberson 2023년 3월 30일
HI,
I want to divide every numeric value in my table by the same number but some columns have strings and some have numbers. I want to convert every number in the table from cm to m (but not the strings of course)
thanks

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 26일
divisor = 2;
T = table(["hello"; "orange"], [1;3], [2; 4])
T = 2×3 table
Var1 Var2 Var3 ________ ____ ____ "hello" 1 2 "orange" 3 4
vt = vartype('numeric')
vt =
table vartype subscript: Select table variables matching the type 'numeric' See Access Data in a Table.
T{:,vt} = T{:,vt} / divisor
T = 2×3 table
Var1 Var2 Var3 ________ ____ ____ "hello" 0.5 1 "orange" 1.5 2
  댓글 수: 3
Siddharth Bhutiya
Siddharth Bhutiya 2023년 3월 30일
Adding on to Walter's answer. Starting 23a there are a lot of standard arithmetic operations that you can directly do on tables to make similar workflows easier. So in 23a you could also use parens indexing instead of brace to get the same results.
T = table(["hello"; "orange"], [1;3], [2; 4]);
vt = vartype("numeric");
T(:,vt) = T(:,vt) ./ 2
T = 2×3 table
Var1 Var2 Var3 ________ ____ ____ "hello" 0.5 1 "orange" 1.5 2
In your case since you have a table with mixed types there is a little more work to be done, however, if you only had numerics in you table then it would be very simple now.
T = table([1;3],[2;4]);
T = T./2
T = 2×2 table
Var1 Var2 ____ ____ 0.5 1 1.5 2
You can read more about math operations on tableshere.
Walter Roberson
Walter Roberson 2023년 3월 30일
@Siddharth Bhutiya -- thanks, that looks useful!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by