How to index a column of data

4 views (last 30 days)
Cam B
Cam B on 20 Feb 2022
Edited: Cris LaPierre on 20 Feb 2022
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

Accepted Answer

Cris LaPierre
Cris LaPierre on 20 Feb 2022
Consider using a combination of the following syntaxes
You can get the result without using a for loop.
  2 Comments
Cam B
Cam B on 20 Feb 2022
That worked for the first part. lowSal=min(match_book{3}, [],2)
How would I go about finding the column number that corresponds to each minimum value? Confused on the second syntax.
Cris LaPierre
Cris LaPierre on 20 Feb 2022
Edited: Cris LaPierre on 20 Feb 2022
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).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!