MATLAB: Editing multiple elements of an array in one line

arrayslist comprehensionMATLAB

My problem is this, I have a huge array and I want to edit multiple elements of the same array that I know the location of. Here's how I would assume this should work based on how matlab handles arrays
h = zeros(10,10,10,2); % just a simple example array
b = [10 5]; % elements I want to place in new location
i = [1 2]; % "locations" I want to place them in
h = h(1,1,1,i) + b(i)
Now what I would expect is for h(1,1,1,1) = 10 and h(1,1,1,2) = 5. But instead what you get is h(1,1,1,1) = [10 5] and h(1,1,1,2) = [10 5]
Why is this? I know a solution is to simple edit them in a loop, however this isn't actually answering the problem.
Thanks

Best Answer

The error you get for your example will probably depend the release. On R2018a, the implicit expansion tries to expand b to fit the 4D selection from h. What I needed to do is make sure that b is 1x1x1x(numel(b)):
h = zeros(10,10,10,2); % just a simple example array
b = [10 5]; % elements I want to place in new location
i = [1 2]; % "locations" I want to place them in
h(1,1,1,i) = h(1,1,1,i) + permute(b(:),numel(size(h)):-1:1);
h(1,1,1,1)
h(1,1,1,2)