Is there a way to access array elements returned by a function directly

Assume this code:
a=magic(4);
then I can do:
a(1,2)
to access the elements of the matrix a. Is there a way to do it directly without associating the variable a with the result of the function magic, that is, something like:
magic(4)(1,2)
?

답변 (2개)

What you've written is not valid MATLAB syntax. You could do something close to what you want using a helper function.
valueAt = @(A, varargin) A(varargin{:});
valueAt(magic(4), 1, 2)
ans = 2
M = magic(4) % so you can see what element (1, 2) is
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

댓글 수: 4

Wow that is complicated, so the matlab way is always to assign the result to a variable even if one only needs some subresult?
If you know you only need the sub-result, then only return that result. But yes, that is how MATLAB works.
It's easy to say how the syntax you described should work in the simple cases that interest you.
It's not nearly so easy to say how it should work in the general case for which we would have to design that feature.
As an example, the eig function has two outputs. The first is a matrix of eigenvectors and the second the corresponding eigenvalues. Suppose I wanted the third eigenvector and eigenvalue? How would you do that with your syntax?
format longg
A = magic(5);
[V, D] = eig(A)
V = 5×5
-0.447213595499958 0.0976400709329798 -0.632976910516004 0.67800972395606 -0.261860649935008 -0.447213595499958 0.352538557500108 0.589480807113999 0.322279530272056 -0.173165093661346 -0.447213595499958 0.550110625795028 -0.391529640194349 -0.550110625795028 0.391529640194349 -0.447213595499958 -0.322279530272056 0.173165093661345 -0.352538557500107 -0.589480807113999 -0.447213595499958 -0.678009723956059 0.261860649935008 -0.0976400709329798 0.632976910516003
D = 5×5
65.0000000000001 0 0 0 0 0 -21.2767654714738 0 0 0 0 0 -13.1262809307092 0 0 0 0 0 21.2767654714738 0 0 0 0 0 13.1262809307092
check = A*V(:, 3)-V(:, 3)*D(3, 3)
check = 5×1
-1.77635683940025e-14 -8.88178419700125e-16 -7.105427357601e-15 -5.32907051820075e-15 -2.66453525910038e-15
Now what would you do to get the third eigenvector and eigenvalue using this call to eig?
[V, D] = eig(A, 'vector')
V = 5×5
-0.447213595499958 0.0976400709329798 -0.632976910516004 0.67800972395606 -0.261860649935008 -0.447213595499958 0.352538557500108 0.589480807113999 0.322279530272056 -0.173165093661346 -0.447213595499958 0.550110625795028 -0.391529640194349 -0.550110625795028 0.391529640194349 -0.447213595499958 -0.322279530272056 0.173165093661345 -0.352538557500107 -0.589480807113999 -0.447213595499958 -0.678009723956059 0.261860649935008 -0.0976400709329798 0.632976910516003
D = 5×1
65.0000000000001 -21.2767654714738 -13.1262809307092 21.2767654714738 13.1262809307092
Like this (the first says all from the first dimensions and the second says only third element in the second dimension):
eig(A, 'vector')(:,3)
Look, i am not criticizing but rather trying to find the way these things are done properly and, most importnatly, efficiently in Matlab especially when it comes to memory. I come from Mathematica and such thing is simple there but that is a different concept.

댓글을 달려면 로그인하십시오.

Matt J
Matt J 2021년 6월 15일
In theory, it is possible to make a function directly indexable using OOP trickery,
>> Magic=IndexableFunction(@magic);
>> Magic{4}(1,2)
ans =
2
It's not worth it...

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2021년 6월 14일

댓글:

2021년 6월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by