Force element access inside for loop

8 次查看(过去 30 天)
Hello, I'm playing with transfer functions to extract the causal part of one.
However, when I do
z = tf('z',0.001); tfCaus = tf(k0plus, 0.001); % adding the delta@0
for stbIdx = find(abs(p)<1)
tfCaus = tfCaus + r(stbIdx)/ (z-p(stbIdx))^(m(stbIdx));
end
I get an error about the ^ (transfer function) operator failing as the inputs are treated as vectors . In particular the error is
In the expression "M^K", the model M must have the same number of inputs and outputs.
while explicitly requesting the index, doing
tfCaus = tfCaus + r(2)/ (z-p(2))^(m(2))
works as expected. It seems to me that stbIdx isn't properly substituted as scalar at runtime. I've checked and and it's a proper vector.
The same behavior fails when doing
zpk([],p(stbIdx)*ones([m(stbIdx),1]), r(stbIdx))
but the code below works
zpk([],p(2)*ones([m(2),1]), r(2))

采纳的回答

Tommy
Tommy 2020-4-28
Is p a column vector?
Notice the difference between the following:
>> for i = 1:3
disp('a')
end
a
a
a
and
>> for i = (1:3)'
disp('a')
end
a
Does it work if you use
for stbIdx = find(abs(p)<1)'
  2 个评论
Massimo Pesavento
Massimo Pesavento 2020-4-28
Thank you so much, indeed that's the cause, I didn't know MATLAB treated them differently.
Have a nice day

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by