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

Answer

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

All matlab Questions

Ask your interview questions on matlab

Write Your comment or Questions if you want the answers on matlab from matlab Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---