How to copy copy column with loop?

3 次查看(过去 30 天)
N K
N K 2012-2-16
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.

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2012-2-16
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))])

C.J. Harris
C.J. Harris 2012-2-16
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)]

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by