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

1 回表示 (過去 30 日間)
bilgesu ak
bilgesu ak 2016 年 2 月 22 日
コメント済み: bilgesu ak 2016 年 2 月 22 日
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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by