【问题标题】:Logical Indexing Failing when Matrix is loaded by matfilematfile加载Matrix时逻辑索引失败
【发布时间】:2018-02-26 12:40:49
【问题描述】:

我有一个存储在.mat 文件中的矩阵,然后通过函数matfile 在matlab 中重新加载。我还有一个逻辑索引,例如logical([1 0 1 0]),我想将其应用于加载的矩阵:

results = matfile('results.mat');
% id is my logical vector of the appropriate size
% IV is a matrix stored in results.mat
newIV = results.IV(:,id);  

但是,我遇到了问题并收到此错误:

'IV' cannot be indexed with class 'logical'. Indices must be numeric.

我不明白是什么导致了这个问题。我之前一直在使用相同的代码并且它正在工作,唯一的事情是我之前不必加载结构结果,我已经在内存中拥有它。 它变得更奇怪了;这行得通:

IV = results.IV;
newIV = IV(:,id); % this works somehow

这也有效:

results_raw = matfile('results.mat');
results = struct('IV',results_raw.IV);
newIV = IV(:,id); % this also works!!! why matlab, why???

我还尝试使用-v7.3 标志重新保存results.mat 文件,但没有解决问题。问题似乎与加载 .mat 文件有关,因为我创建了一个带有矩阵的结构并使用了逻辑索引,它工作正常。

问题:为什么当我将results.IV 传递给IV 时,索引会起作用?我怎样才能使它与results.IV一起工作?

感谢您的帮助!!! :D

【问题讨论】:

  • 似乎你不能像其他限制一样,如matfile页面中明确记录的那样:mathworks.com/help/matlab/ref/matfile.html#bt2ft8s-6当您将数据分配给另一个变量时,它声明为一种新的数据类型,所有工作区中的数据,与matfile无关。

标签: matlab indexing mat-file


【解决方案1】:

正如@Adiel 在问题 cmets 中所说。您不能使用 logical 索引。 因此,使用findlogical 索引转换为numeric 索引。

results = matfile('results.mat');
% id is my logical vector of the appropriate size
% IV is a matrix stored in results.mat
newIV = results.IV(:,find(id));  

【讨论】:

  • 这仅在idresults.IV 具有相同维度时才有效。由于results.IV 是一个矩阵,我们需要id 是一个矩阵,这样find(id) 才能按预期工作。如果id 是一个向量,在线性索引的情况下,那么即使find(id) 也不起作用,如matfile does not support linear indexing。解决方案似乎是将results.IV 复制到工作区中的矩阵中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
相关资源
最近更新 更多