How do I utilize a function with multiple outputs within another function?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I'm ranking values within columns of an array (descending), where the final output is an array with each value's rank in the same position as the original value. I'm able to do this with multiple lines of code as follows:
[~,DR] = sort(vals,'descend'); % DR: index of values in descending order
[~,valsDRank] = sort(DR,'ascend'); % valsDRank: ranks in array position of original value
I'm curious how to accomplish this using a nested sort command in one line of code due to the multiple outputs of the sort function?
Thanks!
7 comentarios
Steven Lord
el 26 de Abr. de 2024
The code that I am working on is going to be shared with my lab, most of whom have an aversion to coding and get intimidated by the sheer number of lines of code and variables in the workspace.
One way to deal with the intimidation factor is to organize the code needed for your workflow into functions rather than scripts. That way you can have a main function that calls other functions, one per step in the workflow, and those functions only accept as inputs or return as outputs the data needed to do their processing or that other functions need from them to do their processing. Any intermediary variables get created inside the function's workspace and go away when the function call ends.
Think of a recipe or the instruction manual for a piece of Ikea furniture or a Lego set. You don't need to think about all the ingredients or all the pieces when you're performing step 3, you just need to worry about what you're actually manipulating in step 3.
function M = muffin(ingredients)
oven = preheatOven();
dryBatter = mixDryIngredients(ingredients);
wetBatter = mixWetIngredients(ingredients);
batter = mixBatters(dryBatter, wetBatter);
muffinPan = preparePan();
muffinPan = fillPan(muffinPan, batter);
M = bakeMuffins(oven, muffinPan);
end
Each step only gets or returns what it worked with. The bowls in which you mixed the ingredients, whether or not you were making blueberry muffins, corn muffins, bran muffins, etc. -- all those are implementation details hidden in the various mix* functions / steps.
Respuestas (1)
Tony
el 26 de Abr. de 2024
A workaround is to avoid the sort function and calculate the ranks yourself by counting how many elements in the array are <= each element. But if the situation becomes more complicated to handle with matrix operations, then you could create a function to avoid the multiple lines in the main code each time
vals = rand(10,1);
valsDRank1 = sum(vals <= vals', 2); % one-line calculation. intermediate nxn matrix is implicitly created during calculations
valsDRank2 = Rank_In_Place(vals); % function call
display([vals valsDRank1 valsDRank2]);
function ranks = Rank_In_Place(vals)
[~,DR] = sort(vals,'descend'); % DR: index of values in descending order
[~,ranks] = sort(DR,'ascend'); % valsDRank: ranks in array position of original value
end
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!