在matlab中将生成的循环行值排列到一个数组中

时间:2016-12-30 14:35:31

标签: matlab scripting

考虑到我已经清楚了解当前这个问题,我编辑了代码以符合我当前的代码问题。所以现在我的代码问题是它不再循环了。如果我放置一个for循环和一些条件,那么应该随每个循环改变的groups的值不会导致循环中k is 2的错误。 groups的初始值来自代码的第一部分,它从一个不同的数组获取其值,但代码的for循环部分应该使用该初始groups值,然后继续从那里改变。这就是问题出现的地方,因为groups的值拒绝改变。

   A = connections;
% Engine
tic
[m, n] = size(A);
groups = [];
ng = 0;

 k=1;
    w= 1:2:3;
    u = unique(A(k,w)); % representation of kth row
    [in, J] = ismember(A(k:end,:),u);
    l = m-k+1;
    r = repmat((1:l).', n, 1);
    c = accumarray([r(in) J(in)],1,[l n]); % count
    c = bsxfun(@min,c,c(1,:)); % clip
    rows = sum(c,2)==2; % check if 2 elements are common
    if any(rows)
        ng = +1;
        groups = (k-1) + [1; find(rows)];
    end


 gr=groups(end);
 nwrry= [A(k,:);A(gr,:)];

for k=1:5
 h=[];
    h(k)= groups(end);
      dff= setdiff(nwrry(end,:),nwrry(end-1,:));
    [rw,cl]= find(nwrry==dff);
    if cl==3
       w=1:2:cl;
    else
        if cl==2
            w=1:cl;
       else
         w=cl:2;
        end
    end
    u = unique(A(h(k),w)); % representation of kth row
    [in, J] = ismember(A(k:end,:),u);
    l = m-k+1;
    r = repmat((1:l).', n, 1);
    c = accumarray([r(in) J(in)],1,[l n]); % count
    c = bsxfun(@min,c,c(1,:)); % clip
    rows = sum(c,2)==2; % check if 2 elements are common
    if any(rows)
        ng = ng+1;
        groups = (k-1) + [1; find(rows)];
    end
       nwrry = [nwrry;A(groups(end),:)]
k=k+1
end

连接是800 x 3阵列。如果你想测试它并看看它给出的结果,请确保该数组与另一行共用的行中最多有2个值。

1 个答案:

答案 0 :(得分:0)

我能够回答这个问题。感谢所有评论过您的建议和帮助的人,因为我确实调查了您所说的内容,并且能够提出有价值的内容。  完整的代码如下;

%% call connections
A= connections;
[m, n] = size(A);%% calculate number of rows (m) and columns(n) in A

%% find rows with similar values as row 1
[a, b]=find(A(1:end,:) == A(1,1));%% Find rows with similar value as the first value of row 1
[a1, b1]=find(A(1:end,:) == A(1,2));%% Find rows with similar value as the second value of row 1
[a2, b2]=find(A(1:end,:) == A(1,3));%% Find rows with similar value as the third value of row 1

%% find the rows with atleast 2 values similar to row 1
int1= intersect(a,a1);%% Find same row numbers between a and a1
int2= intersect(a,a2);%% Find same row numbers between a and a2
int3= intersect(a1,a2);%% Find same row numbers between a1 and a2
 ints=[int1;int2;int3];%% Place all intersections in one array(ints)

%% delete row number that is same with row number 1
   ints= unique(ints);%% delete repeated row numbers 
 [Lia,Locb] = ismember(ints(1:end,:),1);%% Find same row numbers same with row number 1
 [z, x]=find(Lia(1:end,:) == 1);%% Find row numbers equal to 1 in Lia
   ints(z(1:end),:)=[];%% delete row numbers that is same with row number 1

   %% place the searched row number and the similar row number in an array(gr)
  gr=1;%% Place initial row number(1) in one array(gr)
 tg=[a;a1;a2];%% Place all row numbers with similar value as row 1 in one array(tg)
 if isempty(ints)%% conditions when ints is empty
    f= setdiff(tg,gr);%% Find row numbers in tg not present in gr
    gr=[gr;f(1,:)];%% add the similar row number in array gr
 else          %% conditions when ints is NOT empty
    gr=[gr;ints(end)]; %% add the similar row number in array gr
 end

%% place the searched row  and the similar row  in an array(nwrry)
 nwrry= [A(1,:);A(gr(end),:)]; 

 %% Create loop for all rows in A and repeat all processes above
for i=1:m-2
    hrry=[];
    hrry(i)= gr(end);

   [a, b]=find(A(1:end,:) == A(hrry(end),1));
   [a1, b1]=find(A(1:end,:) == A(hrry(end),2));
   [a2, b2]=find(A(1:end,:) == A(hrry(end),3));
   int1= intersect(a,a1);
   int2= intersect(a,a2);
   int3= intersect(a1,a2);
    ints=[int1;int2;int3];
    ints= unique(ints);
 [Lia,Locb] = ismember(ints(1:end,:),gr(1:end,:));
      [z, x]=find(Lia(1:end,:) == 1);
   ints(z(1:end),:)=[];
     tg=[a;a1;a2];

 if isempty(ints)
    f= setdiff(tg,gr);
    if isempty(f)
        Anb=1:m;
        Anbc=Anb';
        nwf= setdiff(Anbc,gr);
        gr=[gr;nwf(1,:)];
    else
        gr=[gr;f(1,:)];
    end
 else
    gr=[gr;ints(end)];
end
nwrry= [nwrry;A(gr(end),:)];
end