Insert array into larger array with unknown number of dimensions

4 views (last 30 days)
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.

Accepted Answer

Rik
Rik on 3 Nov 2023
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 Comment
Kevin Kleiboemer
Kevin Kleiboemer on 3 Nov 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!