How to find first 10 minimum values in a table array?

26 views (last 30 days)
I have a table array with 5 columns (1,2,3,5 are numbers and column 4 have letters). (I am uploading one example of this array) I would like to exctract from this array the first 10 minimum values, depending the number in column 3.
I tried these
but are no use for my purpose.
T=readtable('input.txt');
A=T(:,1);
B=T(:,1);
C=T(:,1);
D=T(:,1);
E=T(:,1);
Minm=min(T,[],1)
Could you please help me?

Accepted Answer

Aritra
Aritra on 30 Jan 2023
Hi,
As per my understanding you are trying to extract the first 10 minimum rows from your input table array, depending on the values in column 3.
To solve this, you can use the B = sortrows(input,3) function to sort the rows of your input table array based on 3rd column values. The direction argument can be used to specify the sorting direction. The default direction argument is set to ‘ascending’.
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1278020/input.txt');
A = sortrows(T,3);
Result = A(1:10,:);
For detail, please see this MathWorks documentation below for more information on sortrows: https://in.mathworks.com/help/matlab/ref/double.sortrows.html#d124e1400369

More Answers (2)

KSSV
KSSV on 30 Jan 2023
Edited: KSSV on 30 Jan 2023
You can sort the array you want and arrange the other arrays/ table into that order. You can pick the first whatever number you want after arraning in the descending order.
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1278020/input.txt') ;
c1 = T.(1) ;
c2 = T.(2) ;
c3 = T.(3) ;
c5 = T.(5) ;
[val,idx] = sort(c3) ;
iwant = T(idx,:)
iwant = 24×5 table
Var1 Var2 Var3 Var4 Var5 ______ ______ ______ __________ ____ 38.965 23.148 40.857 {'NDFNE' } 2.5 38.764 23.334 67.625 {'KJCAKB'} 4 38.371 22.854 90.605 {'B' } 4 38.337 23.34 106.38 {'GG' } 6 38.392 21.824 120.07 {'EEGD' } 4 38.242 22.082 120.33 {'BHSTH' } 5 38.041 23.274 134.41 {'SD' } 6.5 38.253 21.777 134.43 {'UUU' } 5 38.019 22.104 141.67 {'UUUUUB'} 3 38.392 21.436 145.06 {'BF' } 2 39.138 20.993 153.03 {'KVJU' } 4 38.882 24.506 153.89 {'KNEREW'} 4 38.506 21.102 162.66 {'A' } 4 37.663 22.125 178.03 {'G' } 6 37.673 22.038 179.51 {'KKKK' } 4 37.56 22.807 180.5 {'ERTT' } 7.5
  2 Comments
Ivan Mich
Ivan Mich on 30 Jan 2023
Thank you but I would like to pick automnatically the first 10 minimum values of this table. I mean I would like to have the attached output eith a use of a command or function.
38.965 23.148 40.85728 NDFNE 2.5
38.764 23.334 67.6252 KJCAKB 4
38.371 22.854 90.60469 B 4
38.337 23.34 106.3843 GG 6
38.392 21.824 120.0669 EEGD 4
38.242 22.082 120.3349 BHSTH 5
38.041 23.274 134.4077 SD 6.5
38.253 21.777 134.4266 UUU 5
38.019 22.104 141.6738 UUUUUB 3
38.392 21.436 145.0648 BF 2
Could you please help me?
Askic V
Askic V on 30 Jan 2023
Edited: Askic V on 30 Jan 2023
Okay, but can you give us example, what is that you expect from this particular example. You know, by definition it cannot exist 10 minimum values. I thought you need 10 values from one particular column, but when you say from the table, I'm not sure what that means.
T = readcell('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1278020/input.txt');
column = 3; % column by which you want to sort
T_sort = sortrows(T, column); % sort by column
T_sort(1:10,:) % show first 10 values
ans = 10×5 cell array
{[38.9650]} {[23.1480]} {[ 40.8573]} {'NDFNE' } {[2.5000]} {[38.7640]} {[23.3340]} {[ 67.6252]} {'KJCAKB'} {[ 4]} {[38.3710]} {[22.8540]} {[ 90.6047]} {'B' } {[ 4]} {[38.3370]} {[23.3400]} {[106.3843]} {'GG' } {[ 6]} {[38.3920]} {[21.8240]} {[120.0669]} {'EEGD' } {[ 4]} {[38.2420]} {[22.0820]} {[120.3349]} {'BHSTH' } {[ 5]} {[38.0410]} {[23.2740]} {[134.4077]} {'SD' } {[6.5000]} {[38.2530]} {[21.7770]} {[134.4266]} {'UUU' } {[ 5]} {[38.0190]} {[22.1040]} {[141.6738]} {'UUUUUB'} {[ 3]} {[38.3920]} {[21.4360]} {[145.0648]} {'BF' } {[ 2]}
You can just enter column = 3 in my previous code and display 10 values from the matrix

Sign in to comment.


Askic V
Askic V on 30 Jan 2023
Another solution is to use readcell function instead of read table.
T = readcell('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1278020/input.txt'); % read from file
column = 1; % column by which you want to sort
T_sort = sortrows(T, column); % sort by column
T_sort(1:10, column) % show first 10 values
ans = 10×1 cell array
{[37.1780]} {[37.3690]} {[37.3980]} {[37.4160]} {[37.5600]} {[37.5730]} {[37.6050]} {[37.6630]} {[37.6730]} {[37.6980]}

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!