Wie kann ich eine Funktion auf viele verschiedene Inputs anwenden und diesen Prozess automatisieren?
5 views (last 30 days)
Show older comments
Hallo zusammen,
ich bearbeite mit Matlab gerade große Datensätze. Dafür habe ich verschiedene Funktionen geschrieben (hier beispielhaft: yield, pax). Damit ich nicht jeden Input in jede Funktion eingeben muss, würde ich den Prozess gern automatisieren. Ziel ist es am Ende folgende Tabelle zu erhalten:
Spalte 1: carrier 1, carrier 2, ...
Spalte 2: yield 1 (input: carrier 1), yield 2 (input: carrier 2), ...
Spalte 3: pax 1 (input: carrier 1), pax 2 (input: carrier 2), ...
Ich habe es bereits mit arrayfun probiert; erhalte jedoch jedes Mal die Fehlermeldung 'not enough input arguments'.
Ich hoffe es kann mir hier jemand helfen.
Gruß,
Sandra
0 Comments
Answers (1)
Madhav Rajan
on 23 Jul 2015
Diese Antwort ist in englischer Sprache, um Ihnen die schnellstmögliche Antwort zu liefern.
I understand that you want to automate the process of passing inputs to your functions. Assuming that 'pax' and 'yield' are two user defined functions that accept one input, you can define two annonymous functions 'f and 'g' that also take exactly one input.
%Sample input
inputs = [1;2;3;4;5;6];
%Annonymous Functions f and g. You can also define them in a file.
f = @(x) x+1; % function f takes in 1 input and returns its increment
g = @(x) x.^2; %function g takes in 1 input and returns the square of the number
%Using arrayfun with your inputs to display them in the appropriate format
result=[inputs, arrayfun(f, inputs), arrayfun(g, inputs)];
If you are using a function that is stored in a function file, you should use the '@' symbol before the function name when using the 'arrayfun' function. Assuming, there is a function 'function1' in a file called 'function1.m' which returns the decrement of its input as shown: function [y] = function(x) y=x-1; end
You can then call 'function1' along with the functions 'f' and 'g' using "arrayfun" as follows:
result=[inputs, arrayfun(f, inputs), arrayfun(g, inputs), arrayfun(@function1, inputs)];
You can also automate the processes without "arrayfun" provided that you pass the correct number of input parameters.
result=[inputs, f(inputs), g(inputs), function1(inputs)];
You can refer the following link for more information regarding the "arrayfun" function:
Hope this helps.
See Also
Categories
Find more on Optics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!