How to Concatenate two matrix's each elements ?

6 ビュー (過去 30 日間)
Lenin chakravarthy Kuppannan
Lenin chakravarthy Kuppannan 2016 年 5 月 17 日
編集済み: Stephen23 2016 年 5 月 20 日
Hi, I am trying to concatenate two matrix's each elements, like following,
A = [ a b c; d e f; g h i] and B = [ 1 2 3; 4 5 6; 7 8 9]
Now i need to create a new matrix like c = [ a1 b2 c3; d4 e5 f6; g7 h8 i9]
Note: here, "a1" is not multiplying 'a' and '1' ( != a * 1). Just concatenating the two elements.
Pls help to do it.
Have a nice day.
- Lenin

採用された回答

Elias Gule
Elias Gule 2016 年 5 月 17 日
Let's try:
syms a b c d e f g h i % initialize symbols
A = [a b c;d e f;g h i];
B = [1 2 3;4 5 6;7 8 9];
sz = size(A);
C = cell(sz); % initialize a cell array to hold the concatenated elements
for row = 1 : sz(1)
for col = 1 : sz(2)
a = A(row,col);
b = B(row,col);
C{row,col} = strcat(a,num2str(b)); % convert B(i,j) to string for concatenation
end
end
  2 件のコメント
Lenin chakravarthy Kuppannan
Lenin chakravarthy Kuppannan 2016 年 5 月 18 日
編集済み: Lenin chakravarthy Kuppannan 2016 年 5 月 18 日
Hi, Thank you for your help. your program works very well. I modified your program little to meet my objective.
My objective is to create a new matrix num_mat = [111 222 333; 444 555 666; 777 888 999] by concatenating the following matrix A = [1 2 3; 4 5 6; 7 8 9] and B = [11 22 33; 44 55 66; 77 88 99].
Note: A,B & num_mat matrix are numbers.
Here the output 'num_mat's elements need to be numbers, not strings.
So i wrote a program with your program as base as following, kindly see the attachment.
- Kindly hep me to solve this problem.
Have a good day.
- Lenin
Elias Gule
Elias Gule 2016 年 5 月 20 日
Ola!
Thanks, your example matrices just made my life easy. Try the following code:
C = arrayfun(@(x,y) str2double(strcat(num2str(x),num2str(y))),A,B)
where A and B are numerical matrices of the same size.

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 17 日
編集済み: Andrei Bobrov 2016 年 5 月 18 日
A = [ 1 2 3; 4 5 6; 7 8 9];
B = [ 1 2 3; 4 5 6; 7 8 9];
str2double(strcat(arrayfun(@num2str,A,'un',0),arrayfun(@num2str,B,'un',0)))

カテゴリ

Help Center および 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