0

I do not understand the following code:

rect2rng = @(pos,len)ceil(pos):(ceil(pos)+len-1);

From the the previous link:

BoundingBox Matlab

Community
  • 1
  • 1

1 Answers1

2

An anonymous function is a short-hand way of defining a function. It accepts input arguments (pos and len) and produces a result.

The general format is:

func = @(input,arguments)some_action(input, arguments)

This creates an anonymous function called func and can then be used (just like any other function) by passing it input arguments

value1 = 1;
value2 = 2;
output = func(value1, value2)

The long-form function equivalent above the above example would be:

function output = func(input, arguments)
    output = some_action(input, arguments);
end

So with this in mind, we can break down the anonymous function in your question into a normal function

function output = rect2rng(pos, len)
    output = ceil(pos):(ceil(pos) + len-1);
end

So based on this, it rounds pos up to the nearest integer using ceil and then creates an array of length len starting as this rounded value.

So if we pass it some test inputs we can see it in action.

rect2rng(1.5, 3)
%// [2  3  4]

rect2rng(1, 3)
%// [1  2  3]

rect2rng(10, 5)
%// [10  11  12  13  14]
Suever
  • 64,497
  • 14
  • 82
  • 101