How can I use conv() in a function handle and keep original vector size, but not use 'same'/'valid'?
2 views (last 30 days)
Show older comments
Kevin Jansen
on 24 Jan 2022
Edited: Kevin Jansen
on 24 Jan 2022
Hi! I am trying to use conv() inside a function handle, while keeping the original vector size of one of the vectors:
So in the example code I convolve a and b, which returns c as [1,4,10,16,22,28,27,18].
I want this to be [1,4,10], as it is the size of a.
This is my code, which does not work:
a = [1,2,3]
b = [1,2,3,4,5,6]
c = @(x) conv(a,b(x(1)))(1:numel(a))
The following does work, but requires multiple lines (which unfortunately is not possible with function handles, so not an option for me):
c = conv(a,b)
c = c(1:numel(a))
If I use 'same' or 'valid', the output is not the same as using conv() and keeping the first columns of the original vector.
using 'same' returns: [16,22,28], using 'valid' returns: [ ], both not [1,4,10].
All help would be appreciated! :)
0 Comments
Accepted Answer
Walter Roberson
on 24 Jan 2022
a = [1,2,3]
b = [1,2,3,4,5,6]
FirstN = @(V,N) V(1:N)
c = @(x) FirstN(conv(a,b(x(1))), numel(a))
However... you are using x(1) as an index into b, and x(1) is a scalar, so b(x(1)) would be a scalar, and you would be doing conv() of a vector and a scalar. The result of convolution of a vector and a scalar is the same as multiplying the vector by the scalar. With a = [1 2 3], you cannot get out [1 4 10] with any scalar b(x(1)) as that is not linear scaling.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!