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

1 Ansicht (letzte 30 Tage)
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..

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by