-1

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

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
4

1 回答 1

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
于 2013-10-12T20:36:00.817 回答