Code covered by the BSD License

### Highlights from Almost a ternary conditional operator

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

# Almost a ternary conditional operator

### Richie Cotton (view profile)

18 Feb 2008 (Updated )

Function for vectorised if-else statements.

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)
16 May 2011 James

### James (view profile)

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.

Comment only
16 May 2011 James

### James (view profile)

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

Comment only
12 Oct 2009 Jan Simon

### Jan Simon (view profile)

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].

Comment only
19 Feb 2008 Mukhtar Ullah

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

Comment only
19 Feb 2008 Yi Cao

Why not use these two sentences:

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

Comment only
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;

Comment only
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.

28 Jul 2008

1. New functionality

2. Code optimisation based upon review comments.

31 Jul 2008

Bug fix: Infinite inputs now handled correctly.

08 Dec 2008 1.1

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