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

1 次查看(过去 30 天)
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..

采纳的回答

Guillaume
Guillaume 2016-2-22
编辑:Guillaume 2016-2-22
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 个评论
bilgesu ak
bilgesu ak 2016-2-22
Thank you Guillaume very much. I applied method 2 and it is working...
Really this answer was perfect...
Regards..

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by