Create array with integral values

10 Ansichten (letzte 30 Tage)
Francesco Primerano
Francesco Primerano am 9 Feb. 2021
Hi everyone,
i have a vector of 3366 elements. Basically i have a nan value every 66 elements. I want to create a new vector that cointains the integration (trapz) of the first 66 elements as the first value, than the integral from the 68 to the 134 as the second value and so on. How can this be achivied ? Could anyone please help me ?

Akzeptierte Antwort

the cyclist
the cyclist am 9 Feb. 2021
Bearbeitet: the cyclist am 9 Feb. 2021
% original vector
v = rand(1,3366);
r = reshape(v,66,[]);
% You may need to do a step where you remove the last row of r, which has the NaNs.
r(end,:) = [];
trapz(r)
ans = 1×51
30.6321 30.0928 30.3872 27.5168 32.7206 26.7639 33.0282 31.8406 35.3154 29.4117 36.6030 32.3742 34.9626 31.9944 30.6600 33.6818 33.8455 31.0136 31.2125 32.2833 32.6725 30.4109 31.4081 31.9513 30.2909 32.1654 34.3995 32.8930 33.0413 33.6475
  3 Kommentare
the cyclist
the cyclist am 10 Feb. 2021
Let's look at what I did, and try to understand it together, and see how it differs from what you just said.
I created a vector with 3,366 elements in it. I made up values, and called it v. But you can call it x if you want.
Then I reshaped that vector into a 66x51 array.
Since every 66th value of the vector is NaN, that means that the bottom row of the array is entirely NaN. That's why I removed it. So now the array is 65x51.
Then, I used the trapz function on the array. If you read the documention, you will see that trapz acting on an array input will perform an integration down each column of that array. Therefore, the result is a 1x51 vector, where each element is the integral of the column.
Other than your expectation of 50 values, instead of the 51 I got, I don't see how what I did differs from what you asked.
Francesco Primerano
Francesco Primerano am 10 Feb. 2021
You're right, thank you so much, what tricked me was the fact that values were stored as 1 row and x columns instead of one column and x rows. Thanks again man

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by