Loop input variables into function to get the various output in table

Hello!
I have a function for which i have some variables as input that create output as follows:
function(A,B,C,D,E,F,G) - So I input varibles A thru G and get the below output X thu Z
for i = 1:varout
switch i
case 1
varout{1} = X;
case 2
varout{2} = Y;
case 3
varout{3} = Z;
end
I have for example the following input variables I want to pass thru the function all at once for me to get a table with all the output variables given the input.
A B C D E F G X Y Z
1 2 1 2 6 7 1 =
3 1 1 8 7 4 10 =
1 5 1 2 6 3 8 =
Appreciate all the help in this matter! Thanks so much!

8 件のコメント

Adam Danz
Adam Danz 2022 年 7 月 18 日
The question/goal is not clear.
I see you're using varout which suggests you expect a variable number of outputs. But you mention that the output should be a table so it's not clear how that all ties together.
Also, what are A,B,C,...,G? Could you provide examples of these inputs?
I have a feeling you're working with a variable number of inputs and want a single output variable that is a table containing the columns from your key when given the input variables. Is that right?
IDN
IDN 2022 年 7 月 18 日
編集済み: IDN 2022 年 7 月 18 日
Hi Adam,
Thank you for taking the time to look at my question and trying to help me out. Yes you are right! Sorry for the confusing question, I was clearly finding it difficutl to explain.
Here are some input examples:
Function(41,62,92,17,3,7,-2)
Function(41,61,92,18,4,7,-2)
Function(41.5,62,92,16,3,7,-2)
Thanks!
Adam Danz
Adam Danz 2022 年 7 月 18 日
That's helpful. How do those inputs map onto what the outputs should be?
IDN
IDN 2022 年 7 月 18 日
i envision each column is an array (A,B,C...) and each row is a "function run" that places on that same row (X,Y,Z). Therefore the mapping/indexing should be on a row basis
Table/Input Table/Output
A B C D E F G X Y Z
41 62 92 17 3 7 -2 54 3.3 5 Varout of function with the inputs in this row
41 61 92 18 4 7 -2 80 1 -4
41.5 62 92 6 3 7 -2 4 36 25
Something along those lines...
Adam Danz
Adam Danz 2022 年 7 月 18 日
Where do those X,Y,Z values come from?
What should the program do?
IDN
IDN 2022 年 7 月 18 日
Its a very complex code, but as as example something like the below should work (just anything really)
X = A+B+C+D+E+F+G;
Y = X / 2;
Z = X+Y;
Adam Danz
Adam Danz 2022 年 7 月 18 日
That should be easy to implement. If those are your variable names, then that's exactly the code you would use to produce those outputs. But I must admit, I am still quite uncertain of the goal.
IDN
IDN 2022 年 7 月 18 日
So Adam, what I vectors/list of the A-G variables....but want to pass them to the function somehow/systematically instead of typing set or variables individually...maybe a loop or something else...that passes thru every row in the vector/list and on that same row in another table records the output variables.

サインインしてコメントする。

 採用された回答

Voss
Voss 2022 年 7 月 18 日
Something like this maybe?
% your initial table
input_table = array2table([ ...
41 62 92 17 3 7 -2; ...
41 61 92 18 4 7 -2; ...
41.5 62 92 6 3 7 -2; ...
],'VariableNames',{'A' 'B' 'C' 'D' 'E' 'F' 'G'})
input_table = 3×7 table
A B C D E F G ____ __ __ __ _ _ __ 41 62 92 17 3 7 -2 41 61 92 18 4 7 -2 41.5 62 92 6 3 7 -2
% convert to a cell array
inputs = table2cell(input_table)
inputs = 3×7 cell array
{[ 41]} {[62]} {[92]} {[17]} {[3]} {[7]} {[-2]} {[ 41]} {[61]} {[92]} {[18]} {[4]} {[7]} {[-2]} {[41.5000]} {[62]} {[92]} {[ 6]} {[3]} {[7]} {[-2]}
% pass each row of inputs to your function, and
% collect the outputs in another cell array
N = size(input_table,1);
outputs = cell(N,3);
for ii = 1:N
[outputs{ii,:}] = your_function(inputs{ii,:});
end
outputs
outputs = 3×3 cell array
{[ 220]} {[ 110]} {[ 330]} {[ 221]} {[110.5000]} {[331.5000]} {[209.5000]} {[104.7500]} {[314.2500]}
% convert the outputs to a table
output_table = cell2table(outputs,'VariableNames',{'X' 'Y' 'Z'})
output_table = 3×3 table
X Y Z _____ ______ ______ 220 110 330 221 110.5 331.5 209.5 104.75 314.25
function [X,Y,Z] = your_function(A,B,C,D,E,F,G)
X = A+B+C+D+E+F+G;
Y = X / 2;
Z = X+Y;
end

2 件のコメント

IDN
IDN 2022 年 7 月 19 日
Thank you so much!
Voss
Voss 2022 年 7 月 19 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020a

タグ

質問済み:

IDN
2022 年 7 月 18 日

コメント済み:

2022 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by