How to Concatenate two matrix's each elements ?
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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
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)))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!