How do we do operations directly on table elements in matlab?

I am thinking of doing min max normalization of my data where if x is vector the procedure is as follows x = (x- min(x))/(max(x)-min(x)). However I want to do this operation directly on the table elements which have 150 rows and 42 features and I want to normalize each of these columns. Is there any one line procedure to do it without using for loop or something like that ? I know we could use curly and dot bracket indexing but the result is not gonna be a table though.

 채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 2일
normalized_data = normalize(data, 'range'); %works on a table, normalizes to [0 1]

추가 답변 (1개)

bharath pro
bharath pro 2020년 7월 2일
If you can use z-score instead of min max, then you can directly do
data=normalize(data);
If you cant do that, then this works in one line.
data = (data - min(data, [], 1)) ./ max(data - min(data, [], 1), [], 1)

댓글 수: 4

D_coder
D_coder 2020년 7월 2일
편집: D_coder 2020년 7월 2일
no this doesn't work. The second line i mean,
It says
Invalid data type. First argument must be numeric or logical , and
This makes sense because min(data, [], 1) doesnot give you a table.
the first line is not useful as I want to do min max normalization
I missed the table part. If your table is homogenous you can convert to array do the computation and convert it back.
data = array2table((table2array(data) - min(table2array(data), [], 1)) ./ max(table2array(data) - min(table2array(data), [], 1), [], 1))
This is a workaround and not a direct answer of normalizing table entries.
again I wanna preserve the table columns names I doubt this would do it
data = array2table((table2array(data) - min(table2array(data), [], 1)) ./ max(table2array(data) - min(table2array(data), [], 1), [], 1), 'VariableNames', data.Properties.VariableNames);
However, the solution I posted using normalize() works directly on tables without that bother.

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

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

태그

질문:

2020년 7월 2일

댓글:

2020년 7월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by