我需要將2個矩陣中的列轉換爲same datatype
,以便我可以運行ismember
。一列是矩陣[]
格式,另一列是字符串格式,即我們需要匹配[2000]
和'2000'
。請參閱:MATLAB中的cell2mat轉換(對於ismember)
mat1 = {'aa' [2001] ; 'ex' [10] ; 'ex' [1001] ; 'rt' [4001] ;} ;
mat2 = {'abc' '10' ; 'def' '4001' ; 'ghi' '2001' ; } ;
ismember(cell2mat(mat1(:,2)), cell2mat(mat2(:,2))) % Gives ERROR
%cell2mat(mat1(:,2) works just fine
%cell2mat(mat2(:,2)) is the PROBLEM.
%Final answer
ans = {...
'aa' [2001] 'ghi'; 'ex' [10] 'abc'; 'ex' [1001] 'abc'; 'rt' [4001] 'def';} ;
如果可能,應該欣賞向量化代碼。