我正在尝试向量化以下MATLAB操作:
给定具有索引的列向量,我想要一个具有相同行数和固定列数的矩阵.矩阵用零初始化,并包含索引指定位置的矩阵.
这是我已经写过的脚本示例:
y = [1; 3; 2; 1; 3];
m = size(y, 1);
% For loop
yvec = zeros(m, 3);
for i=1:m
yvec(i, y(i)) = 1;
end
Run Code Online (Sandbox Code Playgroud)
期望的结果是:
yvec =
1 0 0
0 0 1
0 1 0
1 0 0
0 0 1
Run Code Online (Sandbox Code Playgroud)
没有for循环可以实现相同的结果吗?我试过这样的事情:
% Vectorization (?)
yvec2 = zeros(m, 3);
yvec2(:, y(:)) = 1;
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我有两个时域信号(X和Y).我试图计算它们之间的传递函数.我使用函数tfestimate,它返回作为频率函数的传递函数和tfestimate估计传递函数的频率向量.它只返回正频率,因为我的信号并不复杂.我的问题是如何在时域中可视化这个传递函数.我尝试了以下代码,但返回的函数在时域中被反转.我想知道为什么.
x = randn(16384,1); % generate random signal
gaussFilter = gausswin(100);
gaussFilter = gaussFilter / sum(gaussFilter); % Normalize.
y = conv(x,gaussFilter);
y = y(1:length(x)); % truancate the Y to be the same length as X
txy = tfestimate(x,y,1024);
tyx = conj(txy(end:-1:2)); % since tfestimate only returns for positive frequency, I estimate the result for negative frequency as the conjugate of positive frequency.
t = ifft([txy' tyx']); % use inverse fourier to visualize transfer function in time domain.
Run Code Online (Sandbox Code Playgroud)
结果't'不是传递函数,而是一个时间相反的版本.有人可以帮我理解发生了什么吗?谢谢.