Extract a column of a matrix created by a command

3 次查看(过去 30 天)
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!

回答(1 个)

Stephen23
Stephen23 2020-7-27
编辑:Stephen23 2020-7-27
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:

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by