【发布时间】: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无关。