0

I have two arrays:

p1=[sym(1) sym(2)]
p2=[sym(3) sym(4)]

I want to do the convolution of those two lists using conv function.

Matlab outputs the following:

Error using conv2

Invalid data type. First and second arguments must be numeric or logical.

Error in conv (line 43)

c = conv2(a(:),b(:),shape);

Can anyone help me how to deal with that?

tdy
  • 36,675
  • 19
  • 86
  • 83
C.S.
  • 105
  • 4
  • You can use a simple `for` loop to do the multiplication yourself. Flip one of the arrays, then multiply each element with each other element. Besides, you mention `conv()`, but the errors use `conv2()`. – Adriaan Aug 12 '21 at 13:37
  • @Adriaan I want to do the convolutions for very large arrays. This was just an illustration. So the `for` loop is not a good solution.Yes I don't know why the error contains `conv2`! – C.S. Aug 12 '21 at 13:40
  • How would a `for` loop not be a good solution for very large arrays? As far as I know, `conv` contains loops under the hood anyway. – Adriaan Aug 13 '21 at 09:58

1 Answers1

1

Edit 1: i have not symbolic math toolbox so i demonstrated a matrix-wise operation on numeric values to calculate conv, i think this should do the trick on symbolic values either.

the conv function just accept numeric values. there is a way to calculate conv matrix-wise i demonstrate it with an example: assume u and v are as follows :

u =
     1     2     1     3
v =
     2     7     1

>> conv(u,v)
ans =
     2    11    17    15    22     3

instead we could first calculate u'*v, then do some rearranging and summing to calculate conv: so first :

>> c=u'*v
c=
     2     7     1
     4    14     2
     2     7     1
     6    21     3

then we do some rearranging:

>> d=[c;zeros(3,3)]
d =
     2     7     1
     4    14     2
     2     7     1
     6    21     3
     0     0     0
     0     0     0
     0     0     0
>>e= reshape(d(1:end-3),[6,3])
e=  
     2     0     0
     4     7     0
     2    14     1
     6     7     2
     0    21     1
     0     0     3

and finally adding values together :

>> sum(e,2)
ans =
     2
    11
    17
    15
    22
     3

you can write your own code to use "v size" to do it(add (numel(v)*numel(v)) zeros to end of u'*v and so on.)

Hadi
  • 1,203
  • 1
  • 10
  • 20