Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
In MATLAB, conv computes the convolution of two vectors: w = conv(u,v) returns a vector of length length(u)+length(v)-1, and w = conv(u,v,shape) returns only a part of it, chosen by shape (“full”, “same” or “valid”). Because multiplying two polynomials is the same operation on their coefficient vectors, conv is also MATLAB’s polynomial-multiplication function. This guide covers the exact syntax, how the three shapes differ, a worked polynomial example, how to read and check the output, version changes that affect results, and free alternatives in Python.
Ask CASRAI · free to tryQuestion about this guide? Ask CASRAI answers with linked sources. 2 free questions a day, no account.
What conv is used for in research
Convolution combines two sequences so that each output element is a sum of products of shifted input elements. MATLAB’s reference defines the kth element of w as the sum of u(j)*v(k-j+1) over all j that give legal subscripts. Two uses cover most research code:
- Filtering and smoothing a measured signal. Convolving a signal vector with a kernel or impulse-response vector applies that filter to the signal. The same idea underlies finite impulse response filters; see FIR filters in MATLAB for the design side, and SciPy signal processing for the Python equivalent.
- Polynomial multiplication. The MathWorks documentation states that for vectors of polynomial coefficients, convolution is equivalent to polynomial multiplication. This is useful when you are expanding characteristic polynomials or combining numerator and denominator terms, as in transfer-function work in MATLAB.
Two details of the definition matter for every result below. First, conv computes the direct convolution, not an FFT-based one, according to the MATLAB reference. Second, the inputs are vectors; for images and other matrices the related functions are conv2 (two-dimensional) and convn (n-dimensional), both listed under See Also on the conv page. If you need the circular (periodic) form instead of the linear one, that is a different operation covered in circular convolution in MATLAB with cconv and fft/ifft.
How to install or access it
conv is a base MATLAB function: the MathWorks reference page does not require a toolbox for the core function. MATLAB itself is obtained from MathWorks at mathworks.com/products/matlab.html. Licensing terms and pricing are set by MathWorks and are not covered here.
Toolboxes enter only in side features of the same documentation page. The related function convmtx, which builds a convolution matrix, belongs to the Signal Processing Toolbox. C/C++ code generation for conv needs MATLAB Coder, GPU code generation needs GPU Coder, and GPU arrays and distributed arrays need Parallel Computing Toolbox. None of these is needed to call conv on ordinary vectors at the command line or in a script. For a broader view of what the product covers, see what MATLAB is used for in research.
Step-by-step: multiply two polynomials and convolve two signals
The steps below follow the examples in the MATLAB reference page for conv (R2026b documentation, read 2026-11-12). The first walkthrough multiplies x²+1 by 2x+7.
- Write each polynomial as a coefficient vector, highest power first. x²+1 has coefficients 1, 0, 1 (the x term is zero), so
u = [1 0 1];. 2x+7 isv = [2 7];. - Call conv. Enter
w = conv(u,v). - Read the result. MATLAB displays
w = 1×4followed by2 7 2 7. That vector holds the coefficients of 2x³+7x²+2x+7. - Check the length. The inputs have 3 and 2 elements, so the output must have 3+2-1 = 4 elements, which it does.
You can verify this by hand: (x²+1)(2x+7) = 2x³ + 7x² + 2x + 7. The convolution sum performs exactly that expansion on the coefficient vectors.
The second walkthrough is a signal-style convolution from the same page:
- Define
u = [1 1 1];andv = [1 1 0 0 0 1 1];. - Run
w = conv(u,v). - MATLAB returns
1 2 2 1 0 1 2 2 1, a 1×9 vector, because 3+7-1 = 9. The three-element vector of ones acts as a moving sum over the longer vector, which is why each pair of ones becomes the rising and falling 1 2 2 1 pattern.
Choosing the shape argument
The optional third argument decides which part of the full convolution is returned. The default is "full".
"full"returns the whole convolution, of lengthlength(u)+length(v)-1."same"returns the central part, with the same size asu. Which input you place first therefore decides the output length."valid"returns only the parts computed without zero-padded edges. Its length ismax(length(u)-length(v)+1,0), except that whenlength(v)is 0 the length islength(u).
The data table below compares these length rules with the matching SciPy modes and gives the length each produces for a 7-element signal and a 4-element kernel, the sizes used in the MathWorks “central part” example.
Worked example: same and valid on one pair of vectors
The MathWorks reference convolves u = [-1 2 3 -2 0 1 2]; (7 elements) with v = [2 4 -1 1]; (4 elements) using w = conv(u,v,"same") and returns 15 5 -9 7 6 7 -1, a 1×7 vector. It notes that the full convolution would have length 10 and that "same" has the length of u, here 7.
Working the convolution sum by hand from the definition, the full result is -2 0 15 5 -9 7 6 7 -1 2 (10 elements). The "same" output is the central seven elements of that vector, which matches the documented output. The "valid" output is the four elements 5 -9 7 6, the positions where the 4-element kernel overlaps the signal completely, which agrees with the rule 7-4+1 = 4. These full and valid values are derived from the documented definition rather than quoted from the page, so treat them as a cross-check you can reproduce by running the two calls yourself.
Output and how to read it
Length and data type
For "full", count the elements: if the output is not length(u)+length(v)-1, one of the inputs is not the vector you think it is. The inputs may differ in length and data type. The output is single if either input is single; otherwise it is double. Complex values are supported.
Row or column orientation (changed in R2026a)
The documentation records a version change. From R2026a, for conv(u,v) or conv(u,v,"full"), the output is a row vector unless both u and v are column vectors. The documentation gives two cases: row u = [1 0 1] with column v = [2; 7; 4] returns the row 2 7 6 7 4, while column u = [1; 0; 1] with the same column v returns the column 2; 7; 6; 7; 4. Before R2026a, orientation depended on the lengths and orientations of the inputs: the same row [1 0 1] with column [2; 7; 4] gave a 5-by-1 column, while that row with column [2; 7] gave a 1-by-4 row. The "same" and "valid" outputs follow the orientation of u and are unchanged. If a script written for an older release now returns a row where it used to return a column, this is the first thing to check.
Sparse and large-data inputs (R2026b and extended capabilities)
Release R2026b adds support for sparse double, single and logical inputs, which the documentation says can reduce memory use and speed processing when inputs contain many zeros. Sparse gpuArray input is not supported. For tall arrays, inputs must be column vectors; with "full" only one of u or v can be tall, and with "same" or "valid" the vector v cannot be tall. For code generation, shape must be a constant and u and v must be vectors with the same orientation.
Undoing a convolution with deconv
The companion function deconv divides one polynomial or sequence by another. Its documented relationship is y = conv(x,h) + r, where [x,r] = deconv(y,h) returns the quotient x and the remainder r. The reference example takes y = [2 7 4 9] and h = [1 0 1] and returns x = [2 7] and r = [0 0 2 2], which is the polynomial division (2x³+7x²+4x+9) / (x²+1) = 2x+7 with remainder 2x+2.
That gives a quick round-trip test for your own use of conv. Convolving the quotient with the divisor gives conv([2 7],[1 0 1]) = [2 7 2 7], and adding the remainder [0 0 2 2] reproduces y = [2 7 4 9]. From R2023b deconv also accepts a shape argument, a method option and a regularisation factor; its default long-division method requires the full shape, and the least-squares method is described as more stable. For deconvolving measured data such as microscopy images, where noise makes plain division unreliable, see Richardson-Lucy deconvolution.
From vectors to images: conv2
C = conv2(A,B) performs the two-dimensional convolution with the same three shapes. For "full" the output size is size(A)+size(B)-1; "same" returns the central part with the size of A. The form conv2(u,v,A) convolves A with the column vector u down the columns and the row vector v along the rows, and it equals conv2(A,conv2(u(:),v(:)')). In the documented example, A = zeros(10); A(3:7,3:7) = ones(5); u = [1 0 -1]'; v = [1 2 1]; Ch = conv2(u,v,A) returns a 12-by-12 array. The documentation also notes that the variants can differ slightly because of round-off error, so compare results with a tolerance rather than exact equality.
Troubleshooting
The output is the wrong length
Check the shape argument. With "same" the output matches the size of the first input, so conv(v,u,"same") and conv(u,v,"same") have different lengths when u and v differ. With "valid" the output can be empty when the second vector is longer than the first, because max(length(u)-length(v)+1,0) bottoms out at 0.
A row came back where a column was expected
This is the R2026a orientation rule described above. Make both inputs column vectors if you need a column result from "full", or reshape the output explicitly with w(:) so the code does not depend on the release.
The result is a matrix problem, not a vector problem
conv takes vectors. For two-dimensional data use conv2, and for higher dimensions convn.
Results differ from another tool
Compare the full output first, and only then compare the trimmed shapes. The mapping in the table below helps when you are porting code to or from Python.
Expecting a circular result
conv is linear convolution computed directly. If your data are periodic or you are working in the frequency domain, you want circular convolution instead; see the circular convolution guide.
Free alternatives
SciPy provides scipy.signal.convolve(in1, in2, mode='full', method='auto'), whose three modes mirror MATLAB’s shapes. Its method argument can be direct, fft or auto, and the SciPy documentation warns that NaN or Inf values break the FFT method. NumPy has a separate numpy.convolve, covered in numpy.convolve: modes, lengths and examples.
MATLAB conv shape |
SciPy convolve mode |
What is returned |
|---|---|---|
"full" (default) |
'full' (default) |
The complete convolution |
"same" |
'same' |
The central part, sized to the first input |
"valid" |
'valid' |
Only the part computed without zero-padded edges |
For SciPy, 'same' is the size of in1, centred with respect to the full output, which matches the MATLAB description of sizing to the first input. The 'valid' length in SciPy is max(N,M)-min(N,M)+1, which differs from MATLAB’s rule when the second input is longer than the first: MATLAB returns an empty result there, whereas SciPy’s rule depends only on the larger and smaller lengths. Check this difference before porting code that relies on 'valid'.
FAQ
What does conv do in MATLAB?
It returns the convolution of two vectors, and for polynomial coefficient vectors it returns the coefficients of the product polynomial. The output length is length(u)+length(v)-1 for the default full shape.
How do I multiply two polynomials in MATLAB?
Write each as a coefficient vector from the highest power down, then call conv. For x²+1 and 2x+7, conv([1 0 1],[2 7]) returns [2 7 2 7].
What is the difference between full, same and valid?
Full is the entire convolution, same is the central part with the size of the first input, and valid keeps only the part computed without zero-padded edges.
Does conv use the FFT?
No. The MATLAB reference states that conv computes the direct convolution, not an FFT-based one.
Is conv the same as conv2?
No. conv works on vectors; conv2 performs two-dimensional convolution on matrices, with the same full, same and valid shapes.
Why did my conv output change from a column to a row?
From R2026a, the full output is a row vector unless both inputs are column vectors. Reshape the result explicitly if your code needs a fixed orientation.
How do I reverse a convolution?
Use deconv, which returns the quotient and remainder so that y = conv(x,h) + r.
Sources
- MathWorks, conv: Convolution and polynomial multiplication (R2026b documentation), read 2026-11-12.
- MathWorks, deconv: Deconvolution and polynomial division, read 2026-11-12.
- MathWorks, conv2: 2-D convolution, read 2026-11-12.
- SciPy, scipy.signal.convolve, read 2026-11-12.
Data: Output-length rules for MATLAB conv shapes and SciPy convolve modes
Compares how MATLAB conv (full, same, valid) and scipy.signal.convolve (full, same, valid) size their output, with the length each gives for a 7-element first input and a 4-element second input. Compiled from the vendor reference pages for each function.
| Function and shape | Output length rule | Length for 7-element and 4-element inputs (elements) | Note |
|---|---|---|---|
| MATLAB conv, full | length(u)+length(v)-1 | 10 | Default shape |
| MATLAB conv, same | Size of u (central part) | 7 | Output follows the first input |
| MATLAB conv, valid | max(length(u)-length(v)+1,0); length(u) if length(v) is 0 | 4 | Parts computed without zero-padded edges |
| SciPy convolve, full | N+M-1 | 10 | Default mode |
| SciPy convolve, same | Size of in1 (centred) | 7 | Output follows the first input |
| SciPy convolve, valid | max(N,M)-min(N,M)+1 | 4 | Differs from MATLAB when the second input is longer |
Sources
- MathWorks: conv (accessed 2026-11-12)
- SciPy: scipy.signal.convolve (accessed 2026-11-12)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







