Hi guys,
I got 3 matrices that I want to concatenate into one. Each matrix is 4x200. What I want to do is to create a matrix that is like matrix c:
a =
8 1 6 5
3 5 7 6
4 9 2 7
b =
5 4 1 3
7 3 2 8
8 9 6 6
c = [a(:,1) b(:,1);a(:,2) b(:,2);a(:,3) b(:,3);a(:,4) b(:,4)]
c =
8 5
3 7
4 8
1 4
5 3
9 9
6 1
7 2
2 6
5 3
6 8
7 6
I want to use a for loop but I couldn't get it to work. Your help will be appreciated. Thank you!

3 件のコメント

Catherine
Catherine 2018 年 2 月 21 日
編集済み: Catherine 2018 年 2 月 21 日
I have tried:
t = 0;
g = 0;
for i = 1:size(a,2)
for j = 1:size(a,1)
c(t+1,g+1) = [a(j,i) b(j,i)];
t = t+1;
end
t = 0;
g = g+1;
end
g = 0;
But the error is dimension mismatch
per isakson
per isakson 2018 年 2 月 21 日
Why for-loop?
Catherine
Catherine 2018 年 2 月 21 日
I just thought it's the easiest way. If there is other ways, I am happy to try out as well. Thanks.

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2018 年 2 月 21 日

0 投票

Try this:
c = [a(:) b(:)];

4 件のコメント

Catherine
Catherine 2018 年 2 月 21 日
編集済み: Catherine 2018 年 2 月 21 日
Thanks! That's even easier.
Star Strider
Star Strider 2018 年 2 月 21 日
As always, my pleasure!
Probably the easiest way is to create ‘v’ as a matrix, then concatenate it with the others:
v = repmat((1:size(a,1))', 1, size(a,2));
c = [v(:) a(:) b(:)];
Note the transpose operator ('). This creates ‘(1:size(a,1))’ as a column vector, necessary for the correct matrix creation.
This version of ‘v’ should work with any matrix ‘a’, since it is defined by the size dimensions of ‘a’.
Catherine
Catherine 2018 年 2 月 21 日
Great! that works well! Thank you
Star Strider
Star Strider 2018 年 2 月 21 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by