What would be a Matlab function with two arguments that returns a Matrix (as shown) ?
4 views (last 30 days)
Show older comments
Salaijobhaka
on 10 Dec 2017
Commented: Salaijobhaka
on 16 Dec 2017
I would like to create a Matlab function (with two arguments, N = Number of parameter, PO = polynomial order) that returns a matrix as shown in the linked file. Desired output Matrix
3 Comments
Image Analyst
on 10 Dec 2017
I have no idea what those matrices are. It looks vaguely similar to meshgrid output, but it's not. I imagine, from the names of things it might have something to do with polyfit() and polyval(), but I can't figure it out. There is obviously some information missing, either you didn't tell us, or your instructor didn't tell you. So you either get it, or start guessing and trying to figure it out.
Accepted Answer
Salaijobhaka
on 10 Dec 2017
2 Comments
Roger Stafford
on 11 Dec 2017
Edited: Stephen23
on 11 Dec 2017
The example given in Bruno Luong's answer at:
which presumably corresponds to your N=4, PO=3 case, may have the same set of four integers between 0 and 3 whose sum is 3, but the ordering of these is very different from that you give in your manually computed "Desired output Matrix". There does appear to be a comparatively simple algorithm which would reproduce the ordering which you give here. However, I am reluctant to work this out if ordering is not signifcant.
Also note that in your matrix as you show it, there seems to be one line missing. Line 53 with 1 1 1 1 appears to have been left out so that the N=4, PO=4 case has only 69 lines whereas it ought to have 70.
More Answers (2)
Roger Stafford
on 16 Dec 2017
@Salaijobhaka: I finally managed to write the script I mentioned earlier in the comment above. It produces the same ordering as you described in your "Desired output Matrix" file five days ago. It wasn't quite as simple as I thought it would be. It makes important use of the 'cumsum' function for appropriate addressing. The 'round' function is supposed to help protect against round-off errors for very large numbers of rows in the resulting matrix, M.
PO = 4; N = 7; % <-- Choose PO and N
M = zeros(round(prod(N+(1:PO))/prod(1:PO)),PO);
c = 1:N+1;
M(1:N+1,1) = (0:N)';
for ip = 2:PO
s2 = repmat(c(N+1),1,N+1);
s1 = c(N+1)-[0,c(2:N+1)-1];
c = cumsum(c);
d2 = c(N+1)-[0,c(1:N)];
d1 = [d2(2:N+1)+1,1];
for in = 1:N+1
M(d1(in):d2(in),3:ip) = M(s1(in):s2(in),2:ip-1);
M(d1(in):d2(in),2) = M(s1(in):s2(in),1)-M(s1(in),1);
M(d1(in):d2(in),1) = repmat(M(s1(in)),d2(in)-d1(in)+1,1);
end
end
2 Comments
Roger Stafford
on 16 Dec 2017
@Salaijobhaka: Note: I see that unfortunately I have inadvertently interchanged N and PO, so that my N is your PO and my PO is your N, as you have defined them in your comment. I will leave the task of reversing these two variables in the script to you, since I am rather sleepy at the present moment and might make a mistake in that undertaking.
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!