Generating all possible pairs of polynomial?

2 次查看(过去 30 天)
Basically what I want to do is similar to here https://uk.mathworks.com/matlabcentral/answers/267009-generating-all-possible-pairs-of-polynomial-interaction-combinations i.e. I want to generate all possible power polinomial witch each term less than a certain number. My code is here
clear all
clc
%%input;
mu=7;
my=7;
me=7;
delay=1;
degree=2;
totalD=my+mu+me;
A = cell(totalD,1);
[A{1:totalD}] = ndgrid([0:degree]);
for k=1:totalD
polyPow(:,k)=reshape(A{k},[],1);
end
polyPow(sum(polyPow,2)>degree,:) = [];
The problem I am facing now. If the inputs mu,my, and me (also degree) are a bit big, the matrix A becomes very large and I got error "Requested 3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3 (77.9GB) array exceeds maximum array size preference". Anyone has a better idea for my purpose?
Thank you
  2 个评论
Rik
Rik 2018-7-9
Shouldn't you expect a 7x7x7x3 array? Otherwise I don't understand your explanation.
Husni Rois Ali
Husni Rois Ali 2018-7-9
编辑:Husni Rois Ali 2018-7-9
Hi Rik
Thank for your response and sorry for the confusion . The code above working fine for a small value of mu, my, me, and degree. The problem now is if those values are a bit big (as in the code above), the cell A becomes very large. I just to ask if anyone has a better idea than mine?

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-7-9
The big problem is that you are throwing out most of your array. The method I use below does that as well, but much less so, making it faster (except for small cases) and more memory-efficient. There are probably way to make this code better, and it is sorely lacking in comments explaining what I'm doing, but it's getting late. If you need some explanation, just comment below this question and I'll edit in comments.
%~ shared setup ~%
clc,clearvars%better than clear all, better still is to use functions
mu=7;
my=7;
me=7;
delay=1;
degree=2;
totalD=my+mu+me;
%~ my code ~%
v=sort(repmat(1:(totalD+1),1,degree+1))-1;
selection=unique(nchoosek(v,degree+1),'rows');
polyPow=zeros(size(selection,1),totalD);
for row=1:size(selection,1)
for col=1:size(selection,2)
if selection(row,col)~=0
c=selection(row,col);
polyPow(row,c)=polyPow(row,c)+1;
end
end
end
polyPow(sum(polyPow,2)>degree,:) = [];
polyPow=unique(polyPow,'rows');
try
%~ your code ~%
A = cell(totalD,1);
[A{1:totalD}] = ndgrid([0:degree]);
polyPow_old=[];
for k=1:totalD
polyPow_old(:,k)=reshape(A{k},[],1);
end
polyPow_old(sum(polyPow_old,2)>degree,:) = [];
catch
polyPow_old=[];
end
%~ check equivalence ~%
if ~isequal(sortrows(polyPow),sortrows(polyPow_old))
if ~isempty(polyPow_old)
error('oops')
end
end
  1 个评论
Husni Rois Ali
Husni Rois Ali 2018-7-9
Dear Rik
Thank you so much. It does exactly what i wanted.
I was also aware that most of my array are omitted, but just cannot find a way around
Thank you

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Schedule Model Components 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by