如何在两个不同的file.mat八度中比较两个不同的矩阵?

时间:2016-08-21 15:46:58

标签: octave

我需要在两个不同的.mat文件中比较两个矩阵,我的意思是我有两个不同的文件:file1.mat和file2.mat,在每个文件中我有3个矩阵:

File1.mat =(M11,M12,M13) Fileé.mat=(M21,M22,M23)

我需要比较M11和M21:

function [Matrice_Result]= difference ()

  R1=importdata('file1.mat')
  R2=importdata('file2.mat')

  Matrice_Result= R1== R2
endfunction

我发现的错误是:

error: binary operator '==' not implemented for 'scalar struct' by 'scalar struct' operations
error: called from differences at line 6 column 9

如果你能帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:3)

.mat文件中的数据加载到工作区的最简单/最合适的方法是通过load命令。它允许您只将一个变量(您知道的名称)导入工作区。

只需运行load命令即可完成此操作,而无需分配变量:

>> load ('file1.mat', 'M11');
>> load ('file2.mat', 'M21');
>> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        M11         1x3                         24  double
        M21         1x3                         24  double

Total is 6 elements using 48 bytes
>> isequal (M11, M21)
ans =  1

但是,如果执行收集到变量中,则此变量将变为 struct ,其字段名对应于您导入的变量的名称,例如。

>> S1 = load ('file1.mat', 'M11');
>> S2 = load ('file2.mat', 'M21');
>> isequal (S1.M11, S2.M21)
ans =  1
相关问题