matlab中的动态变量大小的数组

时间:2013-10-12 19:48:05

标签: matlab

此代码正常运行但需要进行改进以进行有效计算

anisLoc=[];% Variable sized array
PiezoLoc=[]; % Variable sized array


for i=1:q % Looking in all q
    if ~isempty(LayerProp{i}.c)% if c is not empty then
        anisLoc=[anisLoc,i];
        c{i}=LayerProp{i}.c;
    end
    if ~isempty(LayerProp{i}.e) % if e is not empty then
        %         if LayerProp{i}.e(3,3)
        PiezoLoc=[i;PiezoLoc];
        e{i}=LayerProp{i}.e;
        %         end
    end

end

anisLoc和Piezoloc是可变大小的数组。我将它们设置为最大值q 但是他们改变了大小并且在他们从初始代码产生相同的答案之后不能清空他们!!

anisLoc=zeros(q,1);% Variable sized array
PiezoLoc=zeros(1,q);% Variable sized array

% This loop checks for specific input in data in all 
for i=1:q % Looking in all q
    if ~isempty(LayerProp{i}.c)% if c is not empty
        anisLoc=[i;anisLoc];
        c{i}=LayerProp{i}.c;
    end
    if ~isempty(LayerProp{i}.e) % if e is not empty
        %         if LayerProp{i}.e(3,3)
        PiezoLoc=[PiezoLoc,i];
        e{i}=LayerProp{i}.e;
        %         end
    end

end

1 个答案:

答案 0 :(得分:1)

我相信这应该快得多:

anisLoc=zeros(q,1);% Variable sized array
PiezoLoc=zeros(1,q);% Variable sized array

% This loop checks for specific input in data in all 
k = 0;
m = 0;
for ii=1:q % Looking in all q
    if ~isempty(LayerProp{ii}.c)% if c is not empty
        k = k + 1;
        anisLoc(k)=ii;
        c{ii}=LayerProp{ii}.c;
    end
    if ~isempty(LayerProp{ii}.e) % if e is not empty
        %         if LayerProp{ii}.e(3,3)
        m = m + 1;
        PiezoLoc(m) = ii;
        e{ii}=LayerProp{ii}.e;
        %         end
    end

anisLoc(k+1:end) = [];
PiezoLoc(m+1:end) = [];

end