Streamlining Code for Efficiency

1 次查看(过去 30 天)
Hi all,
Was wondering if there's any way to make this code run faster. This is just a snapshot of the code:
  • I have an array y of size (e.g. 63 rows, 6 columns) made up of just 1s and 0s.
  • I'm applying some logical constraints (> 20) to it (termed as "LogicT")
  • I've put the logic test within the "for" loop, but am wondering how I can place it outside the "for" loop for faster run time (e.g. specifying "i" as a variable?)?
for i = 1:size(y,1)
LogicT{1} = (y(i,1)+ y(i,2)) <=1;
LogicT{2} = y(i,2) == y(i,6);
LogicT{3} = 1 - y(i,3) + y(i,1) + y(i,2)>= 1;
if LogicT{1} == 1 && ...
LogicT{2} == 1 && ...
LogicT{3} == 1
TotalNew(i,:) = y(i,:);
else TotalNew(i,:) = zeros(1,size(y,2));
end
end
Many thanks in advance for your help. And apologies if this is a trivial question.
Best regards, Eugene

采纳的回答

Roger Stafford
Roger Stafford 2016-3-17
TotalNew = bsxfun(@times,(y(:,1)+y(:,2)<=1)&(y(:,2)==y(:,6))&(1-y(:,3)+y(:,1)+y(:,2)>=1),y);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by