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

1 visualizzazione (ultimi 30 giorni)
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..

Risposta accettata

Guillaume
Guillaume il 22 Feb 2016
Modificato: Guillaume il 22 Feb 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 Commento
bilgesu ak
bilgesu ak il 22 Feb 2016
Thank you Guillaume very much. I applied method 2 and it is working...
Really this answer was perfect...
Regards..

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by