How can I calculate trapz value based on an event counter and then plot it?

Hey there,
the following lines of code should implement a calculation of an integral for certain events in the data.
The very first colum is the time, the second the corresponding value and the third column the event counter.
The values are saved in the integ_mtrx. It worked with cumtrapz, but i am having my problems with the trapz function.
Here the events 0 should be calculated. Events with 0 in the second colum shouldn't be calculated ( like event 1,2,3,4,..)
for every event the integral should be calculated:
for i= 0:(vzwcounter)
clear temp;
temp = Integ_MTRX(Integ_MTRX(:,3)==i,:);
B(i,1)= trapz(temp(:,1),temp(:,2));
end

4 Commenti

What is the problem with the shown code?
Remarks: Avoid the useless clear temp command, because in Matlab this is a waste of time only.
There is no need to set vzwcounter in parentheses.
I get the following error:
does this error relate to the impossibility to calculate trapz for the counter values in the first picture eg 1,2,3...
the problem is that i want to skip the calculation of the integral whenever the vzwcounter value in the third column changes after each step. so for instance in the first picture for counter value 1,2,3,...
but i want to calculate the trapz value whenever the values in the third column are not changing for more than one entry. and save the trapz value in matrix B
how do i implement a safe if-condition?
I tried it like this:
for j = 1:1:length_integmtrx
if(Integ_MTRX(j,3)-Integ_MTRX(j-1,3)) < 1
B(i,1)= trapz(temp(:,1),temp(:,2));
end
end
I'm lost. Should I assume that the first image contains the contents of Integ_MTRX? What is the value of vzwcounter? What is the contents of the second image and why is this an integral?
Which are the "counter variables in the 1st image"? What do you expect trapz(temp(:,1),temp(:,2)) to reply, if temp is a [1x2] vector?
Can you post some code which runs by copy&paste?
I attached you the Integ_MTRX and vzwcounter data.
Both images are from Integ_MTRX and show a snapshot from the data.
Based on the data I need to calculate the integral.
trapz(temp(:,1),temp(:,2)) should be skipped whenever the value in the third column, which are the values of vzwcounter, is changing for each entry. vzwcounter is just counting up events in a different function. for each event the integral should be calculated (e.g. event 175 in the second image, first column is the time (x) and second column are the values (y)).

Accedi per commentare.

 Risposta accettata

As far as I understand your code was almost working. I added an if condition to skip scalar data and shifted the index for the output B by 1 because 0 is no valid index in Matlab:
B = nan(1, vzwcounter + 1);
for k = 0:vzwcounter
temp = Integ_MTRX(Integ_MTRX(:, 3) == k, :);
if size(temp, 1) > 1
B(k + 1) = trapz(temp(:, 1), temp(:, 2), 1);
end
end

Più risposte (0)

Richiesto:

il 13 Set 2022

Commentato:

il 16 Set 2022

Community Treasure Hunt

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

Start Hunting!

Translated by