Matlab 组合2个矩阵

Matlab 组合2个矩阵,matlab,matrix,Matlab,Matrix,有: a = [1;2;3;4;5;6;7;8;9;10]; %(10x1 double) b = [1;3;4;5;6;9]; %(6x1 double) 我希望结合a和b。因此,我的预期结果是: 我认为可以使用条件或第一次导入零(102)?你能帮我吗?方法1:条件检查 在填充组合数组之前,检查数组中的值是否匹配。如果它们匹配,则填充两列。如果它们不匹配,则将在数组中放置“NaN”未定义的术语。控制数组b扫描的变量Index仅在找到两个数组之间的匹配时递增 %Method 1: C

有:

a = [1;2;3;4;5;6;7;8;9;10];   %(10x1 double)
b = [1;3;4;5;6;9];   %(6x1 double)
我希望结合
a
b
。因此,我的预期结果是:


我认为可以使用条件或第一次导入零(102)?你能帮我吗?

方法1:条件检查

在填充组合数组之前,检查数组中的值是否匹配。如果它们匹配,则填充两列。如果它们不匹配,则将在数组中放置“NaN”未定义的术语。控制数组
b
扫描的变量
Index
仅在找到两个数组之间的匹配时递增

%Method 1: Conditional%
a = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];  
b = [1; 3; 4; 5; 6; 9];   

%Creating a result array of the same length as array "a"%
Combined_Array = zeros(length(a),2);

%Copying the first column into the result array "Combined_Array"%
for Row = 1: +1: length(a)
Combined_Array(Row,1) = a(Row,1);    
end

%Padding the array "b" with zeroes to match the length of array "a"%
b = [b; zeros(length(a) - length(b),1)];


Index = 1;
for Row = 1: +1: length(a)

%If the values in arrays "a" and "b" do not match%
if a(Row,1) ~= b(Index,1)
Combined_Array(Row,2) = "NaN";
Index = Index - 1;
end

%If the values in arrays "a" and "b" match%
if a(Row,1) == b(Index,1)
Combined_Array(Row,2) = b(Index,1);
end

Index = Index + 1;
    
end

%Printing the result%
Combined_Array
方法2:串联和填充数组

在需要未定义术语
“NaN”
的数组中填写,并相应地连接其余内容。使用
horzcat
将列并排连接在一起。(
vertcat

请重新读取,阅读并提供。“为我实现此功能”在这里是离题的。你必须做出诚实的尝试,然后问一个关于你的算法或技术的具体问题。
%Method 2: Hard-Coded and Concatenation%
a = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];  
b = [1; 3; 4; 5; 6; 9];   

b = [b(1); "NaN"; b(2:5);"NaN"; "NaN"; b(6); "NaN"];


Combined_Array = horzcat(a,b);