フィルターのクリア

Call both outputs of a function in a for loop

2 ビュー (過去 30 日間)
Eli Dim
Eli Dim 2015 年 8 月 7 日
回答済み: Stephen23 2015 年 8 月 7 日
I have a function VirtualPPDispatch which has one input and 2 outputs:
function [gencost, sortedNames] = VirtualPPDispatch(curCell1)
The input curCell1 is a vector which changes row size. An example of curCell1 is the matrix below:
curCell1 = [492,0,0,4.95000000000000,-4.95000000000000,1,100,1,33,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,66.7500000000000,-66.7500000000000,1,100,1,445,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,24,-24,1,100,1,160,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,13.2000000000000,-13.2000000000000,1,100,1,44,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,47.2500000000000,-47.2500000000000,1,100,1,63,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,21.7500000000000,-21.7500000000000,1,100,1,29,0,0,0,0,0,0,0,0,0,0,0,0,6];
After some calculations within the function VirtualPPDispatch I get the outputs: gencost and sortedNames. When I call the function in my main .m file I use the following syntax:
%cost_matrix is filled with the output gencost
cost_matrix = cell(size(GENcostSorted_PmaxPmin_columnsADDED,1), 1);
for i1 = 1:size(GENcostSorted_PmaxPmin_columnsADDED,1)
curCell1 = GENcostSorted_PmaxPmin_columnsADDED{i1};
cost_matrix(i1) = {VirtualPPDispatch(curCell1)};
clear curCell1
end
I don't know how to get the sortedNames out in the same for loop so what I do is I copied the relevant parts where I obtain sortedNames and made a separate function out of it which I call using the following for loop:
%dispatch_seq is filled with the outputs of sortedNames
dispatch_seq = cell(size(GENcostSorted_PmaxPmin_columnsADDED,1), 1);
for i2 = 1:size(GENcostSorted_PmaxPmin_columnsADDED,1)
curCell1 = GENcostSorted_PmaxPmin_columnsADDED{i2};
dispatch_seq(i2) = {SortedNames(curCell1)}; %cell version of mpc.gen matrix
end
This works however, what I am doing is pretty much executing the same function twice which increases the computational time significantly. Could you tell me how I could combine everything in one loop so I do not have to copy and rename the function and call it a second time to get the second output.

採用された回答

Stephen23
Stephen23 2015 年 8 月 7 日
Instead of this:
cost_matrix(i1) = {VirtualPPDispatch(curCell1)};
Try this:
[tmp1,tmp2] = VirtualPPDispatch(curCell1);
cost_matrix{i1} = tmp1;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by