简体   繁体   English

具有匿名功能的Linspace每秒跳过一次值

[英]Linspace with anonymous function skipping every second value

I have a really simple problem but can't get it done. 我有一个非常简单的问题,但无法完成。

I want to produce a linspaced sequence between two values of an anonymous function. 我想在一个匿名函数的两个值之间产生一个linspaced序列。 If I do it with values its no problem and looks like this 如果我用价值观做到这一点,那就没有问题,看起来像这样

n = 5;
left = 1;
right = 3;

y = zeros(n, 1);
x = linspace(left, right, n)';

q = zeros(2*n, 1);
q(1:2:end) = x
q(2:2:end) = y

But the same thing is not possible with anonymous functions as boundaries. 但是使用匿名函数作为边界是不可能做到的。 My attempt looks like this but I would really appreciate a better solution 我的尝试看起来像这样,但是我真的很感谢更好的解决方案

n = 5;
left = @(t) 0.5 * t;
right = @(t) 2 * t^2 + 5;
diff = @(t) right(t) - left(t);

q = @(t) [];

for i = 1:n
    q = @(t) [q(t) i*diff(t)/n 0*t];
end

q(0.5)

I hope you can help me, thanks in advance! 希望您能帮助我,在此先感谢!

Thanks to @Adiel I made the following answer 感谢@Adiel,我给出了以下答案

function [Q] = reference_configuration(left, right, n, t)

l = left(t);
r = right(t);
diff = r - l;

x = linspace(0, diff, n);
y = zeros(1, n);

q = zeros(1, 2*n);
q(1:2:end) = x;
q(2:2:end) = y;

end

Maybe it helps someone. 也许它可以帮助某人。 Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM