How do I utilize a function with multiple outputs within another function?

1 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2024-4-26
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.
A "muffin" function could be:
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.
Graham Jones
Graham Jones 2024-4-27
@Steven Lord I appreciate the response, and if it was solely for my own use I wouldn't mind nor care about more lines of code or additional functions. However, 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, and even more so by having to incorporate multiple subfunctions. Getting them to agree to try Matlab was a win for me in and of itself. Some of them care about preheating the oven, some about mixing the dry ingredients, and others the wet ingredients...and so on. All of them want to be in the same room together and attend to their part of the recipe and my only goal was to prevent them from trying to assemble incorrect ingredients together. I guess that I could have been more explicit from the get-go, but @Stephen23's short answer was sufficient and @Walter Roberson's response was perfect.

请先登录,再进行评论。

回答(1 个)

Tony
Tony 2024-4-26
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]);
0.4764 5.0000 5.0000 0.7965 3.0000 3.0000 0.8299 1.0000 1.0000 0.0677 10.0000 10.0000 0.7993 2.0000 2.0000 0.3230 6.0000 6.0000 0.6277 4.0000 4.0000 0.1315 8.0000 8.0000 0.1077 9.0000 9.0000 0.1905 7.0000 7.0000
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

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by