Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm MATLAB中邻接元素的数学表示_Algorithm_Matlab_Math_Combinatorics - Fatal编程技术网

Algorithm MATLAB中邻接元素的数学表示

Algorithm MATLAB中邻接元素的数学表示,algorithm,matlab,math,combinatorics,Algorithm,Matlab,Math,Combinatorics,我有一套元素和可能的相邻组合: 因此,总的可能组合为c=11,可通过以下公式计算: 我可以使用如下的a对其进行建模,其元素可以表示为a(n,c): 我曾尝试在MATLAB中实现这一点,但由于我已硬编码了上述数学,因此对于n>4: n=4; c=((n^2)/2)+(n/2)+1; A=zeros(n,c); for i=1:n A(i,i+1)=1; end for i=1:n-1 A(i,n+i+1)=1; A(i+1,n+i+1)=1; end

我有一套元素和可能的相邻组合:

因此,总的可能组合为c=11,可通过以下公式计算:

我可以使用如下的a对其进行建模,其元素可以表示为a(n,c):

我曾尝试在MATLAB中实现这一点,但由于我已硬编码了上述数学,因此对于
n>4

n=4;
c=((n^2)/2)+(n/2)+1;
A=zeros(n,c); 

for i=1:n 
    A(i,i+1)=1; 
end 

for i=1:n-1 
    A(i,n+i+1)=1;
    A(i+1,n+i+1)=1;
end 

for i=1:n-2 
    A(i,n+i+4)=1;
    A(i+1,n+i+4)=1;
    A(i+2,n+i+4)=1; 
end 

for i=1:n-3 
    A(i,n+i+6)=1;
    A(i+1,n+i+6)=1;
    A(i+2,n+i+6)=1;
    A(i+3,n+i+6)=1;
end

按照我上面的数学公式,是否有一种相对较低复杂度的方法在MATLAB中用集合n的n个元素来转换这个问题?

简单的方法是用第一个
k
位集取一个位模式,并将其下移
n-k
次,将每个移位的列向量保存到结果。那么从

1
0
0
0
换班1、2和3次以获得

|1 0 0 0|
|0 1 0 0|
|0 0 1 0|
|0 0 0 1|
我们将使用它来实现这一点

function A = adjcombs(n)
   c = (n^2 + n)/2 + 1;   % number of combinations
   A = zeros(n,c);        % preallocate output array 

   col_idx = 1;             % skip the first (all-zero) column 
   curr_col = zeros(n,1);   % column vector containing current combination
   for elem_count = 1:n
      curr_col(elem_count) = 1;   % add another element to our combination
      for shift_count = 0:(n - elem_count)
         col_idx = col_idx + 1;   % increment column index 
         % shift the current column and insert it at the proper index
         A(:,col_idx) = circshift(curr_col, shift_count);
      end
   end
end
使用
n=4和6调用函数,我们得到:

>> A = adjcombs(4)
A =

   0   1   0   0   0   1   0   0   1   0   1
   0   0   1   0   0   1   1   0   1   1   1
   0   0   0   1   0   0   1   1   1   1   1
   0   0   0   0   1   0   0   1   0   1   1

>> A = adjcombs(6)
A =

   0   1   0   0   0   0   0   1   0   0   0   0   1   0   0   0   1   0   0   1   0   1
   0   0   1   0   0   0   0   1   1   0   0   0   1   1   0   0   1   1   0   1   1   1
   0   0   0   1   0   0   0   0   1   1   0   0   1   1   1   0   1   1   1   1   1   1
   0   0   0   0   1   0   0   0   0   1   1   0   0   1   1   1   1   1   1   1   1   1
   0   0   0   0   0   1   0   0   0   0   1   1   0   0   1   1   0   1   1   1   1   1
   0   0   0   0   0   0   1   0   0   0   0   1   0   0   0   1   0   0   1   0   1   1

你真正的问题是什么?看起来你已经有了一个解决方案,不是吗?你是在问这将如何扩展到其他任意集吗?是的,我已经有了一个解决方案,但我对MATLAB是新手,我想在MATLAB中将其作为一个算法进行编程。如何编程?您首先需要弄清楚如何将其扩展到其他任意集(无需Matlab)。我想我看到了这将如何推广,但还不完全清楚(特别是可能的相邻组合的顺序)。在上面的示例和下面的MATLAB代码中,我已经对n=4进行了推广。但我想对任何n个元素都这样做。我的代码不能处理n个元素。如果您建议我如何为n中的任何数字做这件事,我将不胜感激。MATLAB代码:clc n=4;c=((n^2)/2)+(n/2)+1;A=零(n,c);对于i=1:na(i,i+1)=1;i=1时结束:n-1A(i,n+i+1)=1;A(i+1,n+i+1)=1;i=1时结束:n-2a(i,n+i+4)=1;A(i+1,n+i+4)=1;A(i+2,n+i+4)=1;i=1时结束:n-3a(i,n+i+6)=1;A(i+1,n+i+6)=1;A(i+2,n+i+6)=1;A(i+3,n+i+6)=1;endI刚刚编辑了OP的问题,以包含他在评论中输入的代码,但似乎您已经为OP的问题提供了完整的解决方案。我不知道他为什么不接受你的回答。