【问题标题】:Array for Loop Matlab [closed]循环 Matlab 的数组 [关闭]
【发布时间】:2013-06-20 12:17:12
【问题描述】:
function f = lowpassFIR(sample)

%Calculates Finite Impulse Response low pass filter coefficient
%using the windowing methods as well

passEdge = 100;
StopbandAtt = 20;
passbandRip =.05;
transWidth = 10;
Fs = sample;





%Step One: select number of coefficients%
deltaF = transWidth/Fs;

%Normalize for each window


rectN = round(0.9/deltaF);
hannN = round(3.1/deltaF);
hammN = round(3.3/deltaF);
blackN = round(5.5/deltaF);


rectPos = rectN/2;
rectNeg = (rectPos*-1);


%For the Vector Array
%rect = rectNeg:rectPos;

deltaSum= passEdge + (transWidth/2);
deltaF2= deltaSum/Fs;

for i = [rectPos:rectNeg]

   %iterate through each value and plug into function in for loop
   %each output of the function will be stored into another array
f= 2*i;
end





end

我已对其进行了编辑,并认为这会起作用,但我收到了错误...

lowpassFIR 中的错​​误(第 6 行) passEdge = 100;

在调用期间未分配输出参数“f”(可能还有其他参数) “/Users/sergiogonzalez-palavicini/Documents/MATLAB/lowpassFIR.m>lowpassFIR”。

【问题讨论】:

  • 不是很清楚....
  • 查看this post,它可能会有所帮助
  • 这取决于函数,如果它是矢量化的,那么你可以简单地做Y = f(rect)。你能发布实际的功能吗?

标签: arrays matlab for-loop


【解决方案1】:

您可以使用arrayfun:

>>> function a = func(b)
> a = b*2;
> end
>>> arrayfun(@func,1:10)
ans =

    2    4    6    8   10   12   14   16   18   20

>>>

它的第一个参数是一个函数,第二个参数是一个数组。
它返回另一个数组,并将函数应用于原始数组的每个元素。
Documentation

【讨论】:

  • 如果 OP 希望每个输出存储在不同的数组(单元格)中,您必须使用 'UniformOutput', false 选项。
  • 现在应该更清楚了!
猜你喜欢
  • 2018-03-07
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 2021-01-11
  • 2013-12-17
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多