How to find first 10 minimum values in a table array?
조회 수: 21 (최근 30일)
이전 댓글 표시
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?
채택된 답변
Aritra
2023년 1월 30일
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
추가 답변 (2개)
KSSV
2023년 1월 30일
편집: KSSV
2023년 1월 30일
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
Askic V
2023년 1월 30일
편집: Askic V
2023년 1월 30일
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
2023년 1월 30일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!