Filter löschen
Filter löschen

How to copy copy column with loop?

3 Ansichten (letzte 30 Tage)
N K
N K am 16 Feb. 2012
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.

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 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))])

C.J. Harris
C.J. Harris am 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)]

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by