How can I create a new matrix from selected number of columns of an existing matrix
1 view (last 30 days)
Show older comments
Hugo
on 27 Jan 2022
Answered: Benjamin Thompson
on 27 Jan 2022
If I have a vector
x=[1,2,3,5]
and a matrix A with size 5*10
how can I get, concatenate and save the columns of the matrix A that are stated in the vector x, in a new matrix, called B
In my example above, if the first 5 columns of A are [2,3,4,5;4,5,4,2;7,5,6,4;5,6,5,4;3,4,3,4] ans must be:
B= [2,3,4,5;4,5,4,2;7,5,6,4;3,4,3,4]
I.e., the 4th column of the matrix A has been surpress in the writing of the matrix B
0 Comments
Accepted Answer
Benjamin Thompson
on 27 Jan 2022
x =
1 2 3 5
>> A = rand(5,10)
A =
0.8147 0.0975 0.1576 0.1419 0.6557 0.7577 0.7060 0.8235 0.4387 0.4898
0.9058 0.2785 0.9706 0.4218 0.0357 0.7431 0.0318 0.6948 0.3816 0.4456
0.1270 0.5469 0.9572 0.9157 0.8491 0.3922 0.2769 0.3171 0.7655 0.6463
0.9134 0.9575 0.4854 0.7922 0.9340 0.6555 0.0462 0.9502 0.7952 0.7094
0.6324 0.9649 0.8003 0.9595 0.6787 0.1712 0.0971 0.0344 0.1869 0.7547
>> A(:,x)
ans =
0.8147 0.0975 0.1576 0.6557
0.9058 0.2785 0.9706 0.0357
0.1270 0.5469 0.9572 0.8491
0.9134 0.9575 0.4854 0.9340
0.6324 0.9649 0.8003 0.6787
0 Comments
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!