How to index a column of data
조회 수: 1 (최근 30일)
이전 댓글 표시
The problem asks for us to find the lowest salary for each worker and the find the column number associated with the lowest salary. A cell array called match_book has been created where the first element is the names, the second element is the IDs, and the third element is salary. The data table for salary is given below where rows correspond with each worker while the column designates a year. Using the min function and the match_book variable, generate a vector with the lowest salary and another vector with the corresponding column number (1 through 7). The code for finding lowest salary is below. Cannot find the corresponding column number.
78 88 56 67 85 96 63
45 98 42 79 55 61 74
56 80 45 83 70 51 44
73 42 90 86 82 55 43
98 91 82 56 94 77 72
98 96 59 81 98 68 87
49 81 97 79 73 61 96
99 86 42 49 48 90 47
98 85 66 47 49 75 74
69 63 63 70 55 73 68
88 79 86 98 91 95 40
48 50 88 60 55 57 60
65 83 51 75 89 86 49
95 41 69 53 54 85 88
for j=1:14
lowSal(j)=min(match_book{3}(j,:)); %outputs lowest salary
end
댓글 수: 0
채택된 답변
Cris LaPierre
2022년 2월 20일
Consider using a combination of the following syntaxes
You can get the result without using a for loop.
댓글 수: 2
Cris LaPierre
2022년 2월 20일
편집: Cris LaPierre
2022년 2월 20일
The second syntax, the one with M and I in the output, returns the min value in M and the corresponding index number to I (when dim is 2, the index is the column number). If you click on the hyperlink, it will take you to the corresponding description in the documentation page. You might also find this example helpful (it is for dim=1, though).
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!