我想要渲染一些matlab代码。这是原始代码:
DelPos=find(q>I);
From=[0 DelPos]+1;
To=[DelPos I+J]-1;
L=cell(J,1);
UC=zeros(1,J);
for j=1:J
L{j}=q(From(j):To(j));
if ~isempty(L{j})
UC(j)=sum(r(L{j}));
end
end
这是我尝试对其进行矢量化:
DelPos=find(q>I);
From=[0 DelPos]+1;
To=[DelPos I+J]-1;
L=cell(J,1);
UC=zeros(1,J);
L{1:J}=q(From(1:J):To(1:J));
UC(1:J)=sum(r(L{1:J}));
但是,它会出错:
The right hand side of this assignment has too few values to satisfy the left hand side.
Error in ParseSolution (line 37)
L{1:J}=q(From(1:J):To(1:J));
更新
我最终用2-dim数组替换了单元格,但是仍然无法对代码进行矢量化:
L=zeros(J,3000);
UC=zeros(1,J);
lenr=zeros(1,J);
for j=1:J
ro=q(From(j):To(j));
lenr(j)=length(ro);
L(j,1:lenr(j))=ro;
if ~isempty(L(j,1:length(ro)))
UC(j)=sum(r(L(j,1:length(ro))));
end
end
我的矢量化版本如下,但ro=q(From(1:J):To(1:J)
)总是被最后一个J代替:
ro=q(From(1:J):To(1:J));
lenr(1:J)=length(ro(1:J));
L(1:J,1:lenr(1:J))=ro(1:J);
UC(1:J)=sum(r(L(1:J,1:length(ro(1:J)))));