Borrar filtros
Borrar filtros

How to index a column of data

1 visualización (últimos 30 días)
Cam B
Cam B el 20 de Feb. de 2022
Editada: Cris LaPierre el 20 de Feb. de 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

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 20 de Feb. de 2022
Consider using a combination of the following syntaxes
You can get the result without using a for loop.
  2 comentarios
Cam B
Cam B el 20 de Feb. de 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 el 20 de Feb. de 2022
Editada: Cris LaPierre el 20 de Feb. de 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).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by