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

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.

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.

1 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 Sep 2 '10 at 16:02 5 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 Sep 2 '10 at 16:52 +1 Obscure and great! I was hoping it was possible.

– gary comtois Sep 3 '10 at 4:06 Bear in mind that the intermediate variable is still fully created though. So if the purpose is to save memory by not having to create a temporary local variable, no luck. – Sam Roberts Sep 21 '11 at 16:18 1 @SamRoberts: You can't really get around that in a strict-evaluation language like Matlab.

The main reason people want this is conciseness/readability, not memory savings. – Mechanical snail Oct 17 '11 at 7:24.

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.

That's a bit disappointing. Oh well, thanks for the quick response. – Joe Kearney Sep 2 '10 at 12:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions