Insert array into larger array with unknown number of dimensions

1 回表示 (過去 30 日間)
Kevin Kleiboemer
Kevin Kleiboemer 2023 年 11 月 3 日
コメント済み: Kevin Kleiboemer 2023 年 11 月 3 日
Hello,
I want to evaluate measurement data with varying number of input parameters. Each parameter results in a dimension of my measured datapoints. There are at least five, but up to eight parameters, ergo dimensions of my data points. When reading the data from its file, I need to loop through two always existing dimensions (the left ones). In each loop, I read an array, that has three to six dimensions. So it looks like
for n=1:nparameter(1)
for m=1:nparameter(2)
data(n,m,:) = datasource.(parameter_a(n)).(parameter_b(m));
end
end
Now, the data(n,m,:) only allows a single dimension vector to be inserted, but the datasource will have multiple dimensions. Using the code above results in the error message
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
How can I read my data fields? I know the number of dimensions and their size a few lines, before this code is executed.

採用された回答

Rik
Rik 2023 年 11 月 3 日
It will probably hurt performance, but you could calculate the linear indices with sub2ind.
Alternatively, you can use this trick:
data = zeros(10,10,3,4,5);
trailing = repmat({':'},1,ndims(data)-2);
data(1,1,trailing{:}) = ones(1,1,3,4,5);
disp('it works :)')
it works :)
  1 件のコメント
Kevin Kleiboemer
Kevin Kleiboemer 2023 年 11 月 3 日
Thank you so much. The second solution works flawlessly. my last attempt was close to your solution, but wrong, so I thought, it would be a dead end. Seeing your code makes very much sense.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by