Streamlining Code for Efficiency

1 Ansicht (letzte 30 Tage)
eugene479
eugene479 am 17 Mär. 2016
Kommentiert: eugene479 am 18 Mär. 2016
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

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 17 Mär. 2016
TotalNew = bsxfun(@times,(y(:,1)+y(:,2)<=1)&(y(:,2)==y(:,6))&(1-y(:,3)+y(:,1)+y(:,2)>=1),y);
  1 Kommentar
eugene479
eugene479 am 18 Mär. 2016
Perfect! Works like a charm. Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by