Hello! I have a matrix in it the number of steps per time. Is it possible to integrate each column of the matrix layer by layer (layer sizes may differ)
MatrixHelpStep=rand(30,40);
layer=(1:5:30); % for example, I took layer 5 (but it can change)
% trying to create a loop for layers
for i=1:length(layer)-1
Imhs=trapz(MatrixHelpStep(layer(i):layer(i)+1))
end
it gives me only one value

댓글 수: 4

"layer" is usually a term used for the third dimension, but your array is 2D. But in MatrixHelpStep(layer(i):layer(i)+1) you are indexing it with a single index.
As layer is maximum 30 and 30 is the maximum number of rows, we would suspect that
Imhs=trapz(MatrixHelpStep(layer(i):layer(i)+1,:))
but watch out for which dimension you wish to trapz() over.
Lev Mihailov
Lev Mihailov 2020년 7월 17일
편집: Lev Mihailov 2020년 7월 17일
this is what I want to get, now I am doing it all manually and I want to write a loop
x=MatrixHelpStep(:,1);
x1=trapz(1:5,1);
x2=trapz(5:10,1);
x3=trapz(10:15,1);
%
x=MatrixHelpStep(:,2);
x4=trapz(1:5,2);
x5=trapz(5:10,2);
x6=trapz(10:15,2);
%
x=MatrixHelpStep(:,3);
x7=trapz(1:5,3);
x8=trapz(5:10,3);
x9=trapz(10:15,3);
Walter Roberson
Walter Roberson 2020년 7월 17일
Could you confirm that you want overlap? trapz(x(1:5,1)) uses 5 elements to form the output, but trapz(x(5:10,1)) uses 6 elements to form the output. Are you wanting 1:5, 6:10, 11:15, 16:20 and so on?
Lev Mihailov
Lev Mihailov 2020년 7월 17일
yes, I want such overlaps

댓글을 달려면 로그인하십시오.

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2020년 7월 17일

0 개 추천

For this I would try cumtrapz - it calculates the cumulative (sp?) trapezoidal integral in the same manner as cumsum calculates the cumulative sum along an array. Doing that would give you the cumulative integrals of your matrix with the same size as your matrix. That way you can easily calculate every following integral between 2 boundaries from that one just by taking the difference between the corresponding boundary-elements. Something like this:
I = peaks(40);
I = I(1:30,:);
ciI = cumtrapz(I);
i10to17ofI = ciI(17,:) - ciI(10,:);
HTH

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품

질문:

2020년 7월 17일

답변:

2020년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by