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:
- to store magic(4) in a variable: M=magic(4)
- 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!
0 个评论
回答(1 个)
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!