How to know dimensions to give in trapz function

Hi,
Let's say I have a matrix (see below). How do I know what number to put in the dimension variable? Matlab just gives some odd explanation without telling how to determine the dimension. Could someone please help to understand how to get the value for dim?
[Kx, W, Ky] = meshgrid(kx, w, ky);
for j = 1:9;
functione = exp(-((W-w_o).^2)/deltaW.^2).*exp(-(Kx.^2+Ky.^2)/(deltaK.^2)).*exp(1i.*sqrt((W/c).^2-(Kx.^2+Ky.^2)).*z(j));
normw = trapz(functione.^2,dim)*dw
normkx = trapz(functione.^2,dim)*dkx
normky = trapz(functione.^2,dim)*dky

2 Comments

It is up to you along which dimension you want it to integrate
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

let's say you have a column vector x and you calculate y according to y = ax^2
now if a is a vector, you will get a matrix y:
x = linspace(0,10,5)';
a = [1 10 100];
y = a.*(x.^2)
y =
1.0e+04 *
0 0 0
0.0006 0.0063 0.0625
0.0025 0.0250 0.2500
0.0056 0.0563 0.5625
0.0100 0.1000 1.0000
This y matrix represents 3 possible column vectors ax^2 with different parameter value
Now to integrate correctoly, you should integrate along the first dimension (same dimension as the x column vector)
dim = 1;
A = trapz(x,y,dim)
A =
1.0e+04 *
0.0344 0.3438 3.4375

7 Comments

So because x is a one dimensional matrix (i.e. it has only one column but 10 rows) the dim=1? What is x had only 1 row but 10 columns, dim would still be 1?
TADA
TADA on 12 Jul 2019
Edited: TADA on 12 Jul 2019
Then the correct dimension would be 2
Actually in this example because x is one dimensional, you don't need to specify the dimension, trapz would use the first dimension that is not 1 in size.
But in a different senario, let's say you have an x matrix and an equal size y matrix describing several data series for instance, you'd probably want to declare the dimension
J K
J K on 12 Jul 2019
Edited: J K on 12 Jul 2019
And in which case dim=3? I know sometimes it happens. Though I never saw dim=4.
It all depends on the data, matrices can have many dimensions...
I understand it all now. Thank you so much for your extensive answers!

Sign in to comment.

Asked:

J K
on 11 Jul 2019

Commented:

on 19 Sep 2019

Community Treasure Hunt

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

Start Hunting!