|
From: | Przemek Klosowski |
Subject: | Re: Mstrix dimesions not matched |
Date: | Wed, 10 Oct 2018 12:37:42 -0400 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
On 10/10/2018 04:16 AM, jin law wrote:
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(:,:,3) = 0 0 896 0
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.
|
[Prev in Thread] | Current Thread | [Next in Thread] |