Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I index a MATLAB array returned by a function without first assigning it to a local variable?

For example, if I want to read the middle value from magic(5), I can do so like this:

M = magic(5); value = M(3,3); 

to get value == 13. I'd like to be able to do something like one of these:

value = magic(5)(3,3); value = (magic(5))(3,3); 

to dispense with the intermediate variable. However, MATLAB complains about Unbalanced or unexpected parenthesis or bracket on the first parenthesis before the 3.

Is it possible to read values from an array/matrix without first assigning it to a variable?

like image 980
Joe Kearney Avatar asked Sep 02 '10 12:09

Joe Kearney


People also ask

How do you return an index to an array in MATLAB?

In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.

What is the index of the first element in an array in MATLAB?

In most programming languages, the first element of an array is element 0. In MATLAB, indexes start at 1. Arrays can be sliced by using another array as an index.

Can you index a function in MATLAB?

MATLAB supports dot indexing into function call results, as in foo(arg). prop . Other forms of indexing into function call results (with parentheses such as foo(arg)(2) or with curly braces such as foo(arg){2} ) are not supported.

What is the index of an array in MATLAB?

Every variable in MATLAB® is an array that can hold many numbers. When you want to access selected elements of an array, use indexing. Using a single subscript to refer to a particular element in an array is called linear indexing.


2 Answers

It actually is possible to do what you want, but you have to use the functional form of the indexing operator. When you perform an indexing operation using (), you are actually making a call to the subsref function. So, even though you can't do this:

value = magic(5)(3, 3); 

You can do this:

value = subsref(magic(5), struct('type', '()', 'subs', {{3, 3}})); 

Ugly, but possible. ;)

In general, you just have to change the indexing step to a function call so you don't have two sets of parentheses immediately following one another. Another way to do this would be to define your own anonymous function to do the subscripted indexing. For example:

subindex = @(A, r, c) A(r, c);     % An anonymous function for 2-D indexing value = subindex(magic(5), 3, 3);  % Use the function to index the matrix 

However, when all is said and done the temporary local variable solution is much more readable, and definitely what I would suggest.

like image 181
gnovice Avatar answered Sep 18 '22 13:09

gnovice


There was just good blog post on Loren on the Art of Matlab a couple days ago with a couple gems that might help. In particular, using helper functions like:

paren = @(x, varargin) x(varargin{:}); curly = @(x, varargin) x{varargin{:}}; 

where paren() can be used like

paren(magic(5), 3, 3); 

would return

ans = 16 

I would also surmise that this will be faster than gnovice's answer, but I haven't checked (Use the profiler!!!). That being said, you also have to include these function definitions somewhere. I personally have made them independent functions in my path, because they are super useful.

These functions and others are now available in the Functional Programming Constructs add-on which is available through the MATLAB Add-On Explorer or on the File Exchange.

like image 31
T. Furfaro Avatar answered Sep 21 '22 13:09

T. Furfaro