creating sub arrays
显示 更早的评论
Hi, I am just going through a MATLAB text book and one of the examples seem to confuse me. I have written the example below. Could some one explain the logic to the displayed results a = [2 3 4; 2 15 6;2 65 4;]; b= a([2 2 2],:) gives the matrix (3x3) 2 15 6 2 15 6 2 15 6
Why does this happen as opposed to 1x9 row vector 2 15 6 2 15 6 2 15 6 ?
I would have thought that we need to explicitly put semicolons to get a 3x3 square matrix because only the semicolon can start a new row for a matrix?? b= a([2;2;2;],:)
Can someone please explain how matlab 'steps into' this code in a real simple way?
回答(1 个)
Matt Fig
2011-2-8
a = [2 3 4; 2 15 6;2 65 4]
b = a([2;2;2],:)
Reads as: take all columns of row 2, three times and assign it to b. Whatever is to the left of the comma is an index into the rows of a, and whatever is to the right of the comma is an index into the columns of a.
Try this:
b = a([2 3],[2,3]) % Take columns 2 and 3 of rows 2 and 3.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!