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

조회 수: 1 (최근 30일)
Graham Jones
Graham Jones 2024년 4월 25일
댓글: Graham Jones 2024년 4월 27일
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