matrixoctave# vertical dimensions mismatch (1x2 vs 1x1)

I am trying to make a simple replacement

my code is the following :

```
grad_f = @(x) [4*x(1).^3 - 8*x(2).*x(1) + 2*x(1) -2;-4*x(1).^2 + 8*x(2)];
x = [3;3]
grad_f(x)`
```

it gives me the following: error: vertical dimensions mismatch (1x2 vs 1x1)

Solution

Within square brackets (the concatenation operator), elements separated by either a comma or a space are concatenated horizontally. This is an unfortunate choice, as it leads to unexpected errors. Always being consistent with spacing around operators (either never use them, or always use them on both sides) will avoid most of these unexpected errors.

For example:

```
[a - 2] % 1x1 array
[a -2] % 1x2 array, same as [a, -2]
[a-2] % 1x1 array
```

This is what is happening in your code:

```
grad_f = @(x) [… + 2*x(1) -2; …];
^^^
```

Either put a space on both sides of the minus operator, or on neither side.

- Assign values based on reverse combination of two columns
- How to split a square matrix into cubes without loops
- Pretty print 2D list?
- Why are quaternions used for rotations?
- Multiple values in one tile with geom_tile
- How to put number of a particular element in a particular row and column constraints in a matrix?
- How to calculate a Kernel/Matrix efficiently
- What's the best way to create a "3D identity matrix" in Numpy?
- How to compute exponential of a matrix inside CUDA thread?
- How to get element-wise matrix multiplication (Hadamard product) in numpy?
- Why does dim=1 return row indices in torch.argmax?
- How to unnest a "list" in a martix?
- Can not update matrix entries for large matrices
- How do I make a matrix from a list of vectors in R?
- Sum two array rows in PHP on a single line
- Get indices of matrix from upper triangle without numpy
- How to read scalar, vector and matrix information in string format resembling Python syntax in R?
- Cache-friendly sqare matrix transposition logic issue
- How to select winner of condorcet election via matrix?
- solve rectangular matrix in python to get solution with arbitrary parameters
- Sum of a list of matrices in R
- Submatrix Ocaml
- Parameterizing type definition at compile time
- How to make a symmetric matrix where the sum of each row and column is k when the diagonal elements are certain (not zero) in excel?
- MPI4PY: Scatter a matrix
- Fill the Diagonal of Each Matrix in a 3D numpy Array with a Vector
- Rotate UVs in Vertex Shader without distorting texture
- Why do I divide Z by W in a perspective projection in OpenGL?
- How to put even numbers from matrix in separate vector in Julia?
- Why does pytorch give an error related to matrix multiplication?