How to locate and extract elements from a matrix?
显示 更早的评论
Hi, I need to locate specific elements in each column of a matrix and print them in a new column matrix. For example if I have a 6x4 matrix
A = [a1 b1 c1 d1;
a2 b2 c2 d2;
a3 b3 c3 d3;
a4 b4 c4 d4;
a5 b5 c5 d5;
a6 b6 c6 d6]
I should locate some elements from the first column of matrix A, and print them into two new column matrices B and C. The first column of matrix A is a1, a2, a3, a4, a5, a6
My new matrix B should take these elements: a1, a2, a3, a4, so
B = [a1; a2; a3; a4]
After that I should define new matrix C, which will take elements a3, a4, a5, a6, so
C = [a3; a4; a5; a6]
This process I need to repeat for every column of matrix A. Does anyone have tips how to do this? Thanks!
4 个评论
the cyclist
2015-12-17
I don't understand the rule for defining B and C, or how to generalize this to another matrix.
Image Analyst
2015-12-17
I agree with the cyclist. Since you haven't given general rules, the best answer is the simplest and what you have already given:
B = [a1; a2; a3; a4]
C = [a3; a4; a5; a6]
Assuming all the a's have been defined in advance, that will give you what you want. If they haven't, you can simply do
B = [A(1); A(2); A(3); A(4)]
C = [A(3); A(4); A(5); A(6)]
Otherwise give rules, because no one knows what they could be.
EB
2015-12-18
Image Analyst
2015-12-18
You've accepted harjeet's answer so we'll assume that his code does the rules you gave above and the problem is now solved.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!