Re:Mstrix dimesions not matched

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re:Mstrix dimesions not matched

jin law

data

1874,1092,2510,1624,1895,1323

2611,1097,3202,2683,2346,1522

3939,1728,3566,2737,3116,1803

3970,1930,3685,2871,3312,2569

4455,1956,3987,2985,4009,2607

4610,1997,4034,3554,4514,2734

4845,2563,4562,4078,4593,2933

5775,3139,5127,4273,4739,4067

5930,3851,5215,4771,5461,4407

6102,4129,6176,5094,5576,5373

6376,4406,6505,5127,5849,5608

6701,4598,6734,5628,5942,6621

6934,4891,7066,6421,6939,6858

7284,4979,7729,6800,6982,7326

7441,5634,7975,7341,7870,7631

7871,6202,8071,7658,8371,7770

8248,7136,8149,8045,8650,8176

8443,7483,8236,8583,8836,8276

8828,7790,8709,8605,9519,8639

8882,9524,9166,9782,9644,9349

 

Data

 

1837,1372,2114,1097,2788,1052

2099,1725,2567,1217,2878,1195

3886,2547,2827,1729,3176,1300

4080,2670,3623,1833,3194,1626

4400,2974,4232,2386,3790,1659

4794,3111,5100,3104,4010,1675

5036,3594,5236,3503,4381,1973

5423,3702,5602,3761,4706,2072

5665,3922,6192,3988,5171,2188

5785,4468,6234,4410,5750,4312

5974,4903,6400,4578,5962,4834

6038,5238,6471,5260,5978,4898

6322,6946,6648,5715,6256,5356

7016,7186,7044,6928,7169,6267

7392,7468,7110,7002,7182,6860

7979,7818,8062,7726,8822,6911

8067,8007,8154,8283,8950,7375

9055,8201,8420,8845,8986,7382

9526,8220,8855,9195,9300,9479

9904,8247,9463,9647,9757,9497

 

 

 

for j=1:6;

y=Data(:,j);

X=[ones(20,1),data(:,j)];

theta(:,j)=round(pinv(X'*X)*X'*y);

Theta=theta(1,1:j);

end

>> Theta

Theta =

 

   -512   1051    105  -1324    205  -1106

 

>> for j=1:6;

y=Data(:,j);

for j=1:6;

X=[ones(20,1),data(:,j)];

theta(:,j,j)=round(pinv(X'*X)*X'*y);

Theta=theta(1,1:j,1:j);

end

end

>> Theta

Theta =

 

ans(:,:,1) =

 

  -3562   1051    105  -1324    205  -1106

 

ans(:,:,2) =

 

     0  -541     0     0     0     0

 

ans(:,:,3) =

 

      0      0  -3648      0      0      0

 

ans(:,:,4) =

 

      0      0      0  -2011      0      0

 

ans(:,:,5) =

 

      0      0      0      0  -2469      0

 

ans(:,:,6) =

 

      0      0      0      0      0  -1106

 

 

I found that the above results I got was not right,

something missing in my code, please help me.

 

Thank,

Jin

 

 

 

 

Sent from Mail for Windows 10

 



Reply | Threaded
Open this post in threaded view
|

Re: Mstrix dimesions not matched

James Sherman
On Wed, Oct 10, 2018 at 4:17 AM jin law <[hidden email]> wrote:

data

1874,1092,2510,1624,1895,1323

2611,1097,3202,2683,2346,1522

3939,1728,3566,2737,3116,1803

3970,1930,3685,2871,3312,2569

4455,1956,3987,2985,4009,2607

4610,1997,4034,3554,4514,2734

4845,2563,4562,4078,4593,2933

5775,3139,5127,4273,4739,4067

5930,3851,5215,4771,5461,4407

6102,4129,6176,5094,5576,5373

6376,4406,6505,5127,5849,5608

6701,4598,6734,5628,5942,6621

6934,4891,7066,6421,6939,6858

7284,4979,7729,6800,6982,7326

7441,5634,7975,7341,7870,7631

7871,6202,8071,7658,8371,7770

8248,7136,8149,8045,8650,8176

8443,7483,8236,8583,8836,8276

8828,7790,8709,8605,9519,8639

8882,9524,9166,9782,9644,9349

 

Data

 

1837,1372,2114,1097,2788,1052

2099,1725,2567,1217,2878,1195

3886,2547,2827,1729,3176,1300

4080,2670,3623,1833,3194,1626

4400,2974,4232,2386,3790,1659

4794,3111,5100,3104,4010,1675

5036,3594,5236,3503,4381,1973

5423,3702,5602,3761,4706,2072

5665,3922,6192,3988,5171,2188

5785,4468,6234,4410,5750,4312

5974,4903,6400,4578,5962,4834

6038,5238,6471,5260,5978,4898

6322,6946,6648,5715,6256,5356

7016,7186,7044,6928,7169,6267

7392,7468,7110,7002,7182,6860

7979,7818,8062,7726,8822,6911

8067,8007,8154,8283,8950,7375

9055,8201,8420,8845,8986,7382

9526,8220,8855,9195,9300,9479

9904,8247,9463,9647,9757,9497

 

 

 

for j=1:6;

y=Data(:,j);

X=[ones(20,1),data(:,j)];

theta(:,j)=round(pinv(X'*X)*X'*y);

Theta=theta(1,1:j);

end

>> Theta

Theta =

 

   -512   1051    105  -1324    205  -1106

 

>> for j=1:6;

y=Data(:,j);

for j=1:6;

X=[ones(20,1),data(:,j)];

theta(:,j,j)=round(pinv(X'*X)*X'*y);

Theta=theta(1,1:j,1:j);

end

end

>> Theta

Theta =

 

ans(:,:,1) =

 

  -3562   1051    105  -1324    205  -1106

 

ans(:,:,2) =

 

     0  -541     0     0     0     0

 

ans(:,:,3) =

 

      0      0  -3648      0      0      0

 

ans(:,:,4) =

 

      0      0      0  -2011      0      0

 

ans(:,:,5) =

 

      0      0      0      0  -2469      0

 

ans(:,:,6) =

 

      0      0      0      0      0  -1106

 

 

I found that the above results I got was not right,

something missing in my code, please help me.

 

Thank,

Jin

 

 

 

 

Sent from Mail for Windows 10

 



Hi Jin,

When asking for help, you need to assume that we don't know what you are trying to do.  What do you expect the output of your code to be?  What do you expect Theta to be, how do you know it is not right?  Also, in general, it is helpful for those that are trying to help you to provide a minimum working example (https://en.wikipedia.org/wiki/Minimal_working_example) that demonstrates the error that you are seeing.  For example, are you seeing the issue with a smaller matrix or only for the size that you provide?

James Sherman Jr.

 


Reply | Threaded
Open this post in threaded view
|

Re: Mstrix dimesions not matched

Przemek Klosowski-7
In reply to this post by jin law
On 10/10/2018 04:16 AM, jin law wrote:

data

1874,1092,2510,1624,1895,1323

Thank you for providing actual example that demonstrates your issue---almost, that is. Please forgive my harping on details: everybody on this list, including myself, really enjoys assisting other people, so I launched into your example trying to say something helpful, but ran into trouble almost from the start---so I decided to use my prerogative as an old fogey to pontificate a little bit. Basically, I am asking you to please help me help you.

In general, I noticed that recently this list had a series of help requests that are hard to act on, which is why I think it's time to step back and think about this whole co-op help arrangement. Basically, we are all using Octave, which is quite powerful but sometimes tricky to use. In other words, we are trying to do something that almost certainly is possible but may require some insight or trick that eludes us, and we need other people to understand what we want, and come up with a suggestion based on their insight and experience.

Therefore, we decide to ask for help. Now, we have two issues: our original puzzle, and then the second problem of explaining it to others in a concise way that would enable others to get on with helping us. That sounds depressing, but the good news is that the second issue is actually a blessing in disguise.  I strongly believe that having to explain something to others is actually a good exercise that improves our own insight in this very thing: I often experienced a sudden flash of inspiration while trying to explain to others what is it that I don't understand.

In particular, let's take your question and restate it in a way that would be easier to act on. First, let's simplify: you use two 6 by 20 data arrays, which is awkward to look at: they can be smaller and easier to deal with; also, instead of just giving their values, I specify them in a way that allows me to load them up:

Data=[  1874,1092,2510,1624;          1895,1323, 2611,1097;        3202,2683,2346,1522]
data=[   1837   1372   2114   1097;   2788   1052   2099   1725;   2567   1217   2878   1195]
 
By the way, I find that having variables (like data and Data or theta and Theta) that have the same name with the only difference being the case of some letters is confusing---and I am easily confused so I would avoid that, personally--but we'll keep them this way for simplicity here.

Then, I think you are showing two code snippets that process the data and asking why the result is different. Note that it is a good practice to indent your code.

for j=1:4;

  y=Data(:,j);

  X=[ones(3,1),data(:,j)];

  theta(:,j)=round(pinv(X'*X)*X'*y);

  Theta=theta(1,1:j);

end


gives Theta =    1194   2459   3150   2521


whereas


for j=1:4;

  y=Data(:,j);

  for j=1:4;

    X=[ones(3,1),data(:,j)];

    theta(:,j,j)=round(pinv(X'*X)*X'*y);

    Theta=theta(1,1:j,1:j);

  end

end


results in Theta being a 1x4x4 3D array, like so:


ans(:,:,1) =    2494   2459   3150   2521
ans(:,:,2) =     0  -597     0     0

ans(:,:,3) =     0     0   896     0
ans(:,:,4) =      0      0      0   2521


Now, this is easier to deal with than your original question: I can cut-and-paste this into my Octave and look into the details.


Firstly, the second way has two nested loops--but the inner loop uses the same variable, so it overwrites the outer loop variable. This is almost never correct--and if it was intended, I would definitely put a lot of  comments near it to explain the logic.


In any case, your theta is constructed as a 2D array in first case, and as 3D array with three indices in the second case, so of course Theta will end up being 2D and 3D, respectively.


Again, you have to explain what are you trying to calculate in each case, and what result do you expect.