MatLab-将函数应用于矩阵中的每一行 [英] MatLab - Applying a function to each row in a matrix

查看:125
本文介绍了MatLab-将函数应用于矩阵中的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,其中包含4个整数的行,并且列数未指定(取决于文本文件).

I have a matrix with rows of 4 integers, with an unspecified number of columns (depends on the text file).

我想将一个函数独立地应用于矩阵的每一行.该功能有4个输入和2个输出.

I'm wanting to apply a function to each row of the matrix, independently. The function has 4 inputs, and 2 outputs.

我正在尝试使用arrayfun函数来执行此操作,但是每当我调用该函数时,都会出现一条错误消息:输入参数不足."

I'm trying to use the arrayfun function to do this, but whenever I call the function, I get an error saying: "Not enough input arguments."

这是函数调用:

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

其中b是n乘4的矩阵.

where b is an n by 4 matrix.

这是函数:

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

gain = 20*log10(OutAmp\InAmp);

phase = (OutPhase - InPhase);

end

任何帮助将不胜感激!

推荐答案

您的函数GainPhaseComp具有4个输入参数,但是您仅传递了1个行向量.具有4个元素的向量仍然是一个变量,而不是4.您需要更改函数定义或拆分向量元素.

Your function GainPhaseComp has 4 input arguments, but you pass only 1 row vector. Vector with 4 elements is still one variable, not 4. You need either to change the function definition or split the vector elements.

第一个选项:

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));

这篇关于MatLab-将函数应用于矩阵中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆