getting a matrix out of a matrix
2 次查看(过去 30 天)
显示 更早的评论
Hi there guys I have a question. How do you get an output out of a matrix? for example here is the matrix:
Q =
10 20 30 40 50 60 70
8 9 10 11 12 13 14
33 30 27 24 21 18 15
28 35 42 49 56 63 70
36 45 54 63 72 81 90
-1 -2 -3 -4 -5 -6 -7
64 69 74 79 84 89 94
and I am required to find:
S=
20 40 60
9 11 13
30 24 18
how do I get it using a single syntax? I've been struggling since I am just new into using Matlab. please help me...
0 个评论
采纳的回答
Andrei Bobrov
2011-8-15
Q(1:3,[2 4 6])
5 个评论
Paulo Silva
2011-8-15
example Q=[1 3;2 4] , I choose the numbers so they are equal to the index positions:
Q(1) is 1
Q(2) is 2
Q(3) is 3
Q(4) is 4
MATLAB allows you to create vectors like this
1:3
ans=
1 2 3
You can use those vector to select certain index values like this:
Q(1:3) % the same as Q([1 2 3])
ans=
1 2 3
Another way to do it is with row and column numbers, notice the comma (,)
Q(1,2) is 3
Q(1,1) is 1
Other things to remember
The word end
Q(end) is the value of the last index, in this case is 4
The :
Q(:) means all values of Q, they appear in rows
The size function
size(Q) is 2 2 this means two rows and two columns
Q(1:2:end) in this case gives 1 3 the meaning is all values at index 1 until the end, the 2 is your step, try 1:2:4
Please read the documentation http://www.mathworks.in/help/techdoc/math/f1-85462.html
更多回答(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!