Azzera filtri
Azzera filtri

How to copy copy column with loop?

3 visualizzazioni (ultimi 30 giorni)
N K
N K il 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.

Risposte (2)

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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by