msmf14
msmf14

Reputation: 1499

Mathematical Operations on 3D Array - MATLAB

I have a mx1xn array (currently m=n=3), and I need to subtract from each column one value stored in a 1x1xn column.

i.e. n represents time, and from each instance of time, I need to subtract a different time-dependent constant from the data (1xm or whatever the dimensions are).

What is the best way to go at it? My attempt wont work:

data(:,2,:) - constants(:,2,:)   %constants in this case is 1x1x3, data is 3x1x3

One attempt would be to loop it, but I'm wondering what is a more efficient way.

Upvotes: 2

Views: 118

Answers (1)

Eitan T
Eitan T

Reputation: 32930

Use bsxfun:

bsxfun(@minus, data, constants)

Upvotes: 2

Related Questions