Matlab:如何使用for循环减少程序的这部分,以及如何避免联合函数的错误消息?

时间:2014-08-04 18:08:35

标签: matlab

如何使用for循环减少程序的这一部分以及如何避免联合函数的错误消息:

V1,V2,...,V10是长度不同的矢量(尺寸(Vj)与尺寸(Vk)不同)

V1 = X(9, ismember(X(1,:), EE(1)));
V2 = X(9, ismember(X(1,:), EE(2)));
V3 = X(9, ismember(X(1,:), EE(3)));
V4 = X(9, ismember(X(1,:), EE(4)));
V5 = X(9, ismember(X(1,:), EE(5)));
V6 = X(9, ismember(X(1,:), EE(6)));
V7 = X(9, ismember(X(1,:), EE(7)));
V8 = X(9, ismember(X(1,:), EE(8)));
V9 = X(9, ismember(X(1,:), EE(9)));
V10 = X(9, ismember(X(1,:), EE(10)));

除了Vj之外,Zj是所有Vk的联盟:

Z1 = union(V2,V3,V4,V5,V6,V7,V8,V9,V10);
Z2 = union(V1,V3,V4,V5,V6,V7,V8,V9,V10);
Z3 = union(V1,V2,V4,V5,V6,V7,V8,V9,V10);
Z4 = union(V1,V2,V3,V5,V6,V7,V8,V9,V10);
Z5 = union(V1,V2,V3,V4,V6,V7,V8,V9,V10);
Z6 = union(V1,V2,V3,V4,V5,V7,V8,V9,V10);
Z7 = union(V1,V2,V3,V4,V5,V6,V8,V9,V10);
Z8 = union(V1,V2,V3,V4,V5,V6,V7,V9,V10);
Z9 = union(V1,V2,V3,V4,V5,V6,V7,V8,V10);
Z10 = union(V1,V2,V3,V4,V5,V6,V7,V8,V9);

??? Error using ==> union
Too many input arguments.

1 个答案:

答案 0 :(得分:1)

您可以将MATLAB cell用于不同大小的向量,然后使用普通的MATLAB for loop

%a cell V
V = cell(10,1);

for i=1:10
    %dummy data of different sizes
    V{i} = rand(i,1);
    %your data 
    %V{i} = X(9, ismember(X(1,:), EE(i)));
end

Union is both associative and commutative因此,您可以按任意顺序对对进行顺序联合,并且它将与一次性所有事物的并集相同:

Z = cell(10,1);
for i=1:10
  for j = 1:10
      if(i~=j)
          Z{i} =  union(V{j}, Z{i});
      end
  end
end