Logical indexing Matrix vs Matrix inside Struct loaded by matfile

1 回表示 (過去 30 日間)
Guilherme Salome
Guilherme Salome 2017 年 9 月 18 日
コメント済み: Walter Roberson 2017 年 9 月 18 日
I have a matrix that was stored in a .mat file, and was then reloaded in matlab via the function "matfile". I also have a logical index, like logical([1 0 1 0]), that I want to apply to the loaded matrix:
results = matfile('results.mat');
% id is my logical vector of the appropriate size
newIV = results.IV(:,id);
However, I am running into a problem and getting this error:
'IV' cannot be indexed with class 'logical'. Indices must be numeric.
I do not understand what is causing this issue. I have been using this same code before and it was working, the only thing was that I did not have to load the struct results before, I already had it in memory. It gets even weirder, this works:
IV = results.IV;
newIV = IV(:,id); % this works somehow
It can get even more weird:
results_raw = matfile('results.mat');
results = struct('IV',results_raw.IV);
newIV = IV(:,id); % this also works!!! why matlab, why???
I also tried resaving the 'results.mat' file using the '-v7.3' flag, but it did not solve the problem. The issue seems to be with loading the .mat file, because I created a struct with a matrix and used logical indexing and it worked fine.
Question: why does indexing work when I pass results.IV to IV? how can I make it work with results.IV?
Thanks for helping!!! :D

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 18 日
matfile is not equivalent to loading the file. matfile() accesses the array "in place", in pieces, loading from the file or writing to the file as needed.
It appears that matfile() does not permit logical indexing. I do not see that documented, but there is no point in fighting it.
newIV = results.IV(:,find(id));
  2 件のコメント
Guilherme Salome
Guilherme Salome 2017 年 9 月 18 日
Found the limitations in this page. If "id" is a vector, in the case of linear indexing, then converting to numeric values, as in "find(id)", will not work, since it is not supported by "matfile". It seems that the most straightforward solution is to just copy "results.IV" into a new matrix in the workspace, and avoid all these restrictions.
Walter Roberson
Walter Roberson 2017 年 9 月 18 日
You are not doing linear indexing as you have : for the first dimension. The find() should work for this.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by