Force element access inside for loop

10 views (last 30 days)
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))

Accepted Answer

Tommy
Tommy on 28 Apr 2020
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 Comments
Massimo Pesavento
Massimo Pesavento on 28 Apr 2020
Thank you so much, indeed that's the cause, I didn't know MATLAB treated them differently.
Have a nice day
Tommy
Tommy on 28 Apr 2020
Fantastic, same to you

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!