2013-03-08 51 views
3

我有一個矩陣,行數爲4個整數,列數未指定(取決於文本文件)。MatLab - 對矩陣中的每一行應用一個函數

我想獨立地將函數應用於矩陣的每一行。該功能有4個輸入和2個輸出。

我想使用arrayfun函數來做到這一點,但每當我調用函數,我得到一個錯誤:「沒有足夠的輸入參數。」

下面是函數調用:

[gain,phase]=arrayfun(@(x) GainPhaseComp(B(x,1:4)), 1:size(B)); 

其中b是由4矩陣的n。

下面是函數:

function [gain,phase] = GainPhaseComp(InAmp,InPhase,OutAmp,OutPhase) 

gain = 20*log10(OutAmp\InAmp); 

phase = (OutPhase - InPhase); 

end 

任何幫助將不勝感激!

+0

可能重複http://stackoverflow.com/questions/2307249/how-to-apply-a-function-to-every -row-in-matlab – gevang 2013-03-08 02:29:15

回答

0

您的函數GainPhaseComp有4個輸入參數,但只傳遞1個行向量。具有4個元素的向量仍然是一個變量,而不是4.您需要更改函數定義或拆分向量元素。

第一種選擇:

function [gain,phase] = GainPhaseComp(inputvector) 
% function body 

第二個選項:

[gain,phase]=arrayfun(@(x) GainPhaseComp(B(x,1),B(x,2),B(x,3),B(x,4)), 1:size(B,1)); 
+0

非常感謝你! – user2049133 2013-03-08 02:02:48

相關問題