Effacer les filtres
Effacer les filtres

How to copy copy column with loop?

3 vues (au cours des 30 derniers jours)
N K
N K le 16 Fév 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.

Réponses (2)

Andrei Bobrov
Andrei Bobrov le 16 Fév 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 le 16 Fév 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)]

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by