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

조회 수: 6 (최근 30일)
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

답변 (1개)

Madhav Rajan
Madhav Rajan 2015년 7월 23일
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
sandra.pnz
sandra.pnz 2015년 7월 24일
편집: sandra.pnz 2015년 7월 24일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Optics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by