Code covered by the BSD License  

Highlights from
Almost a ternary conditional operator

4.0

4.0 | 1 rating Rate this file 1 Download (last 30 days) File Size: 2.06 KB File ID: #18800

Almost a ternary conditional operator

by Richie Cotton

 

18 Feb 2008 (Updated 08 Dec 2008)

Function for vectorised if-else statements.

| Watch this File

File Information
Description

This function has two purposes:
1. Loops of simple if-else statements can be avoided through vectorisation, providing a performance boost.

% Example:
% vif(eye(3), 4, 1)
% ans =
%
% 4.0000 1.0000 1.0000
% 1.0000 4.0000 1.0000
% 1.0000 1.0000 4.0000

Example 2:
% tic; for i=1:100000; if 1; 4; else 1+i; end; end; toc
% Elapsed time is 0.831261 seconds.
%
% truth = rand(1000000,1)< 0.5;
% tic; vif(truth , 4, 1+i); toc
% Elapsed time is 0.097463 seconds.

2. Syntax for individual if-else statements can be simplified, in a manner reminiscent of a C-style ternary conditional operator, at a slight performance loss.

Example 3:
% x = vif(condn, 3, 4);
% % is easier to type than
% if condn
% x = 3;
% else
% x = 4;
% end

Updates: char and infinite inputs now correctly handled.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
18 Feb 2008 Yi Cao

This is a good idea. However, if truth = logical(truth) on line 37 is replaced by truth = ~~truth will improve the speed. You even can remove the if line completely.

18 Feb 2008 Mukhtar Ullah

Good idea. However, I would have written it like this (for efficiency):

if ~islogical(truth)
   truth = logical(truth);
end
out = zeros(size(truth));
out(:) = no;
out(truth) = yes;

19 Feb 2008 Yi Cao

Why not use these two sentences:

out=yes(ones(size(truth)));
out(~truth)=no;

19 Feb 2008 Mukhtar Ullah

Yi,
You are right. I did not realize that the variable 'yes' is already there as input.

12 Oct 2009 Jan Simon

I cannot follow your Example 2:
  for i=1:100000; if 1; 4; else 1+i; end; end
  truth = rand(1000000,1)< 0.5; vif(truth , 4, 1+i);
You compare the speed of the first part, which computes nothing in 1e5 loops, with the 2nd, which creates a vector of 1e6 random [4 +0i] and [1 +1i].

16 May 2011 James

Apologies on my last post, in the second example, it should read "ii=1:1000000".

16 May 2011 James

Sorry, my first post was accidentally deleted.

each of these operations can be accomplished with a single line:

first example:

4*eye(3)+3*~eye(3)

second example (where ii = (1:1000000)' and bb = rand(numel(ii),1)):

(bb>0.5)*4 + (bb<=0.5).*(ii+1);

running this is even faster:

tic; (bb>0.5)*4 + (bb<=0.5).*(ii+1); toc
Elapsed time is 0.072034 seconds.

The third example:

x = condn*3 + ~condn*4;

No function-calling overhead, no new stack frame, and written in a single line.

Please login to add a comment or rating.
Updates
28 Jul 2008

1. New functionality

2. Code optimisation based upon review comments.

31 Jul 2008

Bug fix: Infinite inputs now handled correctly.

01 Aug 2008

Bug fix: char inputs no longer coerced to numeric. More input checking.

08 Dec 2008

Silly bug fixed.
Also now warns if second or third input is complex.

Tag Activity for this File
Tag Applied By Date/Time
ternary Richie Cotton 22 Oct 2008 09:48:09
vectorisation Richie Cotton 22 Oct 2008 09:48:09
utilities Richie Cotton 22 Oct 2008 09:48:09
performance boost Richie Cotton 22 Oct 2008 09:48:09
utilities Cristina McIntire 12 Dec 2008 15:32:56
vector Cristina McIntire 12 Dec 2008 15:37:47

Contact us at files@mathworks.com