How to expand the matrix in Matlab (forming $A$ matrix in least square problem)

74 Views Asked by At

If I have 5 data points (actually, this could be vary huge number):
$a = [1;2;3;4;5]$

To each data point, I can form a matrix $[a(i),2a(i)]$. (actually, I have to find the matrix, this matrix needs some calculation.)

For example, $[a(i)\times 2+3, 3\times a(i)cos \theta]$

Therefore, I can form 5 such matrices with different values.

Now, If I want to form a big matrix:

$A = \begin{bmatrix}1 & 2 \\2 & 4 \\3 &6 \\4&8\\5&10 \end{bmatrix}$.

One way to do it is by for-loop. Is there any compact way to do it? (If number of data points is very large).


I want to use this technique to form the $A$ matrix in the least square problem. And I have 200 measurements.

1

There are 1 best solutions below

2
On

I don't know if I understood you well, but if you have:

  • the data array $\mathbf{x}=\begin{bmatrix}x_{1}\\ x_{2}\\ \vdots\\ x_{n} \end{bmatrix}$
  • the meassurements array $\mathbf{y}=\begin{bmatrix}y_{1}\\ y_{2}\\ \vdots\\ y_{n} \end{bmatrix}$

you can create the matrix: $$\mathbf{A}=\begin{bmatrix}x_{1} & y_{1}\\ x_{2} & y_{2}\\ \vdots & \vdots\\ x_{n} & y_{n} \end{bmatrix}$$ just with the code:

A = [ x, y ] ;

I hope it was usefull to you