belgarion
belgarion

Reputation: 115

Making a matrix from another matrix

I have a matrix with 1 column, x rows. My second matrix has to be the first few values of that matrix concatenated to the last values of that matrix.

Example: a=[0:1:10] b=[0,1,9,10]

Question: How do I build b, by using a?

Upvotes: 1

Views: 48

Answers (2)

drsh
drsh

Reputation: 66

Say the number of values you want to extract off of each end is n. The horzcat command let's you horizontally concatenate matrices.

n=2;
a=[0:1:10];
b=horzcat(a(1:n),a(end-n+1:end))

Upvotes: 1

David Stutz
David Stutz

Reputation: 2628

E.g. for letting b be the first two columns of a and the two last columns of a concatenated: b = [a(:, 1:2), a(:, size(a, 2) - 1:size(a, 2))].

Example:

>> a = [0:1:10]

a =

 0     1     2     3     4     5     6     7     8     9    10

>> b = [a(:, 1:2), a(:, size(a, 2) - 1:size(a, 2))]

b =

 0     1     9    10

Upvotes: 1

Related Questions