Extract a column of a matrix created by a command

3 views (last 30 days)
Udi
Udi on 27 Jul 2020
Edited: Stephen23 on 27 Jul 2020
I have a matrix that was created by a command. Let's say
magic(4)
Now I want to extract a specific elemnt from this matrix. Of course, I can do that using the commands for selecting subsets of elements from the matrix.
So that extracting the 3rd element from magic(4) means:
  1. to store magic(4) in a variable: M=magic(4)
  2. M(3) selects the 3rd element.
My question is whether this could be made using only one command. My first trial was these two, but neither worked:
magic(4)(3)
(magic(4))(3)
Any help would be much appreciated!

Answers (1)

Stephen23
Stephen23 on 27 Jul 2020
Edited: Stephen23 on 27 Jul 2020
MATLAB does not generally support indexing** directly into the output of functions or operations. The usual solution is to allocate to a temporary variable. If you want this feature then please submit an enhancement request.
You could use subsref to do this without a temporary variable, but it would be obfuscated and complex code that you will later regret writing. I do not recommend doing this.
** Version R2019b and later allows dot indexing into function outputs, see:

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!