Wie kann ich eine Funktion auf viele verschiedene Inputs anwenden und diesen Prozess automatisieren?

4 visualizaciones (últimos 30 días)
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

Respuestas (1)

Madhav Rajan
Madhav Rajan el 23 de Jul. de 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.
  1 comentario
sandra.pnz
sandra.pnz el 24 de Jul. de 2015
Editada: sandra.pnz el 24 de Jul. de 2015
Hey Madhav,
thanks for your answer. I understand now how to use arrayfun. I applied your solution to my problem. Unfortunately, the error "You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts." is still popping up. I don't even know what exactly that means. Do you have any idea why the error still occurs?
Thanks again, Sandra

Iniciar sesión para comentar.

Categorías

Más información sobre Optics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by