tags:

views:

89

answers:

3

How can you read from an array/matrix without first assigning it to a 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 with the intermediate variable. Matlab complains about Unbalanced or unexpected parenthesis or bracket on the first bracket before the 3.

Is this possible?

+3  A: 

unfortunately syntax like magic(5)(3,3) is not supported by matlab. you need to use temporary intermediate variables. you can free up the memory after use, e.g.

tmp = magic(3);
myVar = tmp(3,3);
clear tmp
second
Not supported? That's a bit disappointing. Oh well, thanks for the quick response.
Joe Kearney
A: 

Think about the case of the magic(n) function... it's contents depends on its size, correct? It doesn't seem to make sense to expect to read from a given row/column without first defining its size.

More on magic from Matlab. This shows you can to do things like:

sum(Magic(5));
gary comtois
i disagree. this is just to the way matlab works. e.g. in python, range(n) returns a list [1,...n], and it's perfectly fine to index (the return value of) the function call directly, as in range(5)[2], without the use of intermediary variables.
second
Good point. I was just thinking in terms of Matlab - declaring and indexing out a value (from that something) have to be kept separate. I agree that if the syntax was supported, it would make complete sense to index a value from a matrix while it's being created. Thanks for the follow up.
gary comtois
+8  A: 

It actually is possible to do what you want, but only if you 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:

subindex = @(A,r,c) A(r,c);      %# An anonymous function to index a matrix
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.

gnovice
well what do you know! though i agree it's pretty ugly, and probably less readable than a temp-var solution. +1 for impressive obscure matlab knowledge!
second
That's disgusting, but a very clear answer. Good work! Should've guessed there'd be a back way into it. I'll think I'll carry on with the temp variable.
Joe Kearney
+1 Obscure and great! I was hoping it was possible.
gary comtois

related questions