How to extract a vector from a large matrix by index in MATLAB?

无人久伴 提交于 2019-12-08 06:55:01

问题


I am stucked with this issue in MATLAB for long now and I hope to find a help here!

I have a very large matrix with 4 vectors in it (each vector is a column) and 72300 rows .. the first column/vector is the index of the data .. so it is something like this: (example is simplified)

Index, Info1  Info2  Info3
1      2      1      1
1      4      5      3
1      2.5    1.3    8
2      1      4      7
2      4      6      9
2      12     3      7
3      2      6      6
3      2      1      4
3      1      4      7

Q1> How can I extract all Info2 with the index==3 from this matrix?

Q2> Is there any method to rearrange the matrix to be like this?

Index,   Info1,   Info2 Info3   Index   Info1   Info 2  Info 3
 1                              2

and so on ..

I hope to find some help from you guys and many many thanks in advance..

kind regards,


回答1:


Q1 :

For you question 1, I use a simple example because I don't have your variable name.

idx = find( VectorName(:,1) == 3 ) % Find in all row at column 1 where = 3 (Index)

After you have the index of all row where index == 3. So extract info2 value

AllInfo2 = VectorName(idx,3); % Get all value where row == idx and column = 3 (Info2)



回答2:


Q1 >> x = A(A(:1)==3,3); % Info2 for Index==3

Q2 >> B = reshape(A.',1,[]); %Flatten A along the rows



来源:https://stackoverflow.com/questions/20055995/how-to-extract-a-vector-from-a-large-matrix-by-index-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!