How to copy copy column with loop?
3 views (last 30 days)
Show older comments
Dear all.
I have problem to copy the first and last with loop.
I have matrix like
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
So I want to copy and place column in same matrix.
For example, I want to copy and adding head column 3 times and tail column 2 time to get new matrix like
1 1 1 1 2 3 4 5 6 7 8 8 8
1 1 1 1 2 3 4 5 6 7 8 8 8
1 1 1 1 2 3 4 5 6 7 8 8 8
How do code it.
Best.
0 Comments
Answers (2)
Andrei Bobrov
on 16 Feb 2012
A = [ 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8]
out = [A(:,ones(3,1)) A A(:,end*ones(2,1))]
OR
out = A(:,[ones(1,3) 1:end end*(ones(1,2))])
0 Comments
C.J. Harris
on 16 Feb 2012
Using 'repmat' is also a possibility:
A = [ 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8]
out = [repmat(A(:,1),1,3) A repmat(A(:,end),1,2)]
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!