From: M.E.L. on
Wow, you're right ... about everything! This backslash operator will work a LOT better. So let me see if I understand this. I've got some function, Y, that I'm trying to minimize

Y=A - [ (k1)rx + (k2)ry ]

where A, rx, and ry are 480x480 matrices, and k1 and k2 are the parameters I am trying to solve for. I say that Y=0, so now

A = (k1)rx + (k2)ry + k3

From what I've been able to gather from the online help, I need to create a 'design matrix', X, such that

A = X*k

and therefore k = X\A .

The online help says to create a 'design matrix' X:

X = [rx ry ones(size(rx)) ]

which, if I'm understanding this correctly, is an array that essentially has a 480x480 array of 1's and rx and ry stacked next to each other. I'm not exactly sure how or why this is helpful, but I'll just go with it.

Now if we do

k = X\A

it should spit out the values for my coefficients. What I get for k is a 480x1440 that has mostly zeros, except for two rows at 480 and 482, which have values that change as you move from left to right across the array. Am I to assume that these are the coefficients that minimize the function at each point?

Thank you again for your help as I struggle through this!
From: Steven Lord on

"M.E.L. " <schrodingers.lyon(a)gmail.com> wrote in message
news:hsho97$6n5$1(a)fred.mathworks.com...
> Wow, you're right ... about everything! This backslash operator will work
> a LOT better. So let me see if I understand this. I've got some
> function, Y, that I'm trying to minimize
>
> Y=A - [ (k1)rx + (k2)ry ]
>
> where A, rx, and ry are 480x480 matrices, and k1 and k2 are the parameters
> I am trying to solve for. I say that Y=0, so now

In this case, that means you have a system of (480*480) equations in two
unknowns.

> A = (k1)rx + (k2)ry + k3
>
> From what I've been able to gather from the online help, I need to create
> a 'design matrix', X, such that
>
> A = X*k
>
> and therefore k = X\A .
>
> The online help says to create a 'design matrix' X:
>
> X = [rx ry ones(size(rx)) ]
>
> which, if I'm understanding this correctly, is an array that essentially
> has a 480x480 array of 1's and rx and ry stacked next to each other. I'm
> not exactly sure how or why this is helpful, but I'll just go with it.

Not exactly. You want the first row of X to contain [rx(1), ry(1), 1] so
that when you compute [rx(1), ry(1), 1]*[k1; k2; k3] (for the k1, k2, and k3
values you're going to compute) results in the value A(1). Similarly, you
want the p'th row of X to be [rx(p), ry(p), 1] so that [rx(p), ry(p),
1]*[k1; k2; k3] is A(p). Therefore, you want to turn rx and ry into column
vectors. To do that, use colon or RESHAPE. I'll show each of those
techniques in my definition of X:

X = [rx(:), reshape(ry, [], 1), ones(numel(rx), 1)]

We want the last column of X to be a ones vector with the same number of
elements as rx, so I created that using the NUMEL function.

> Now if we do
>
> k = X\A
>
> it should spit out the values for my coefficients. What I get for k is a
> 480x1440 that has mostly zeros, except for two rows at 480 and 482, which
> have values that change as you move from left to right across the array.
> Am I to assume that these are the coefficients that minimize the function
> at each point?

Now if you use the X I created above, k should be 3-by-1.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: M.E.L. on
Thank you, Steven, for clarifying that for me! That makes more sense. But using the X that you suggested, I get an error when I try to do k=X\A:

??? Error using ==> mldivide
Matrix dimensions must agree.

Error in ==> FFT_DataFit3 at 61
k=X\A

X is a 230400x3 matrix and A is 480x480. Have I done something wrong?

Thank you!
From: Steven Lord on

"M.E.L. " <schrodingers.lyon(a)gmail.com> wrote in message
news:hsjta1$ibj$1(a)fred.mathworks.com...
> Thank you, Steven, for clarifying that for me! That makes more sense.
> But using the X that you suggested, I get an error when I try to do k=X\A:
>
> ??? Error using ==> mldivide
> Matrix dimensions must agree.
>
> Error in ==> FFT_DataFit3 at 61
> k=X\A
> X is a 230400x3 matrix and A is 480x480. Have I done something wrong?

No, I missed a step in my explanation. Since you "columnized" your rx and
ry, you also need to do the same to A.

k = X\(A(:))

To check, X*k should be A(:) [at least in a least-square sense.] Since X is
230400-by-3 and k is 3-by-1, the result should be 230400-by-1 which is the
size of A(:).

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


First  |  Prev  | 
Pages: 1 2
Prev: Mean gray scale of video
Next: create an executable