How to extract minimum values in a row and replace them with their variable?
이전 댓글 표시
I have four colums and 928 rows in a table. I would like to extract the smallest number in each row and replace it with the variable (title/name) at the top of the column in which the value came from.
I have managed to extract the smallest number by changing the table into an array. However, I was not able to change that small value into the name of the column. Moreover, I do not prefer changing the table into arrays becasue I would like to have my final result in a table format.
How may I do so? Find the smallest value in a row and replace it with its title in a table?
댓글 수: 8
madhan ravi
2019년 1월 18일
upload your table as .mat file
Alayt Abraham Issak
2019년 1월 18일
Kevin Phung
2019년 1월 18일
its load (‘file name’), so :
load(’Data_Placement.mat’)
if therr are multiple variables then you can pass specific variables in the second arg
madhan ravi
2019년 1월 21일
what is your goal ? illustrate with an example of the output
Alayt Abraham Issak
2019년 1월 21일
madhan ravi
2019년 1월 21일
편집: madhan ravi
2019년 1월 21일
See if the below does what you want , it calculates the minimum of the entire table:
Minimum=min(min(table2array(T),[],'omitnan')) % T your table
Note:Upload you table as .mat file , it's the second time I am asking.
Alayt Abraham Issak
2019년 1월 21일
Alayt Abraham Issak
2019년 1월 22일
답변 (1개)
Kevin Phung
2019년 1월 18일
편집: Kevin Phung
2019년 1월 18일
If you want to have both numeric and strings in your table, those table elements will have to be cell arrays.
for example, if you have :
a = {1 2 3 4 5}; % your data values, stored in cells.
table1 = table(a',a',a')
tableVarNames = {'a','b','c'}; %your column names
table1.Properties.VariableNames = tableVarNames
to change a table element from numeric to a string, running :
table1{1,3} = tableVarNames(1)
will give you:
table1 =
5×3 table
a b c
___ ___ ___
[1] [1] 'a'
[2] [2] [2]
[3] [3] [3]
[4] [4] [4]
[5] [5] [5]
So something like this will find the minimum for each row and replace it with variable names:
for i = 1:size(table1,1)
[M,I] = min(cell2mat(table1{i,:})) %M is the minimum value, I is the indice
table1{i,I} = tableVarNames(I); %changes that value to a variableName
end
댓글 수: 4
Alayt Abraham Issak
2019년 1월 19일
Kevin Phung
2019년 1월 21일
I was just making a trivial table, and how to change the contents of a table with different data types . Did the for loop that I provided not resolve your issue?
Alayt Abraham Issak
2019년 1월 21일
Kevin Phung
2019년 1월 21일
You got the error because your AB table needs to be cell arrays.
Can you try table2cell for your ABTable.
so:
ABTable = table2cell(Subset) % this is now a matrix of cells, NOT a table
tableVarNames = {'Plmt','S107','S108','S11','S112'} % your variable names
for i = 1:size(ABtable,1)
[M,I] = min(cell2mat(ABtable{i,:})) %M is the minimum value, I is the indice
Subset{i,I} = tableVarNames(I); %makes changes to your main table, Subset
end
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

