how to insert a new column to a matrix after its every two column?

1 vue (au cours des 30 derniers jours)
bilgesu ak
bilgesu ak le 22 Fév 2016
Commenté : bilgesu ak le 22 Fév 2016
Hi; I have a matrix as:
A=
1 1 1 1 1 1
5 1 5 1 3 1
4 2 4 2 2 2
2 2 3 3 4 2
3 3 2 3 5 2
7 3 6 4 6 3
and b is like:
1 2 3
3 3 2
1 1 3
5 6 1
1 2 1
5 1 5
I want to add every b's column to A's after every two column and I want to get:
new=
1 1 1 1 1 2 1 1 3
5 1 3 5 1 3 3 1 2
4 2 1 4 2 1 2 2 3
2 2 5 3 3 6 4 2 1
3 3 1 2 3 2 5 2 1
7 3 5 6 4 1 6 3 5
Thanks in advance;
Regards..

Réponse acceptée

Guillaume
Guillaume le 22 Fév 2016
Modifié(e) : Guillaume le 22 Fév 2016
A more generic version of Stephen's method 1:
skipcolumns = 2;
bcols = (1:size(b, 2)) * (skipcolumns+1);
assert(size(b, 2) * skipcolumns == size(A, 2), 'number of columns in A and b don''t match skipcolumns');
acols = setdiff(1:size(b, 2)+size(A, 2), bcols);
new1(:, bcols) = b;
new1(:, acols) = A;
a more generic version of method 2:
skipcolumns = 2;
assert(size(b, 2) * skipcolumns == size(A, 2), 'number of columns in A and b don''t match skipcolumns');
new2 = reshape([reshape(A, size(A, 1)*skipcolumns, []); b], size(A, 1), [])
  1 commentaire
bilgesu ak
bilgesu ak le 22 Fév 2016
Thank you Guillaume very much. I applied method 2 and it is working...
Really this answer was perfect...
Regards..

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by