How to find first 10 minimum values in a table array?
21 views (last 30 days)
Show older comments
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
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
0 Comments
More Answers (2)
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,:)
2 Comments
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
You can just enter column = 3 in my previous code and display 10 values from the matrix
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
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices 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!