How can I use conv() in a function handle and keep original vector size, but not use 'same'/'valid'?

2 Ansichten (letzte 30 Tage)
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! :)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Jan. 2022
a = [1,2,3]
a = 1×3
1 2 3
b = [1,2,3,4,5,6]
b = 1×6
1 2 3 4 5 6
FirstN = @(V,N) V(1:N)
FirstN = function_handle with value:
@(V,N)V(1:N)
c = @(x) FirstN(conv(a,b(x(1))), numel(a))
c = function_handle with value:
@(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 Kommentare
Kevin Jansen
Kevin Jansen am 24 Jan. 2022
Bearbeitet: Kevin Jansen am 24 Jan. 2022
Thanks! I will try this out. You are right, I wrote my example wrong. For the convolution in the example I actually did conv(a,b). In my code b is actually equal to poisspdf(1:188,x(1)), that's why I put x(1) there, Thanks again!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by