How do I integrate this set of matrix data points

조회 수: 7 (최근 30일)
vishav Rattu
vishav Rattu 2017년 2월 24일
편집: Massimo Zanetti 2017년 2월 24일
I have a matrix diffphi which is a 32x32 matrix. I want to integrate this using trapz in the following manner:
for every row namely k, I want to integrate diffphi(k,1:2) over 1:2 and , then diff(k,1:3) over 1:3, .... diff(k,1:32) over 1:32 then store all values in a new matrix called ophi which will be a 32x32 matrix. How should I do it?
Here is something that I tried:
if true
% code
end
ophi = zeros(32,32);
for h = 1:32
for d = 1:32
c = 1:d;
ophi(h,d) = trapz(c,diffphi(h,(1:d)));
end
end
But it shows the following error:
Error in ipermute (line 23) inverseorder(order) = 1:numel(order); % Inverse permutation order
Error in trapz (line 73)
Thanks in advance. if ~isempty(perm), z = ipermute(z,perm); end

채택된 답변

Massimo Zanetti
Massimo Zanetti 2017년 2월 24일
You just need one for loop as trapz operates also on matrices. This is the piece of code:
A = rand(32);
B = zeros(32);
for c = 1:size(A,2)
B(:,c) = trapz(1:c,A(:,1:c),2);
end
B
  댓글 수: 2
vishav Rattu
vishav Rattu 2017년 2월 24일
I need to integrate every row, vary the indices and store it in a 32x32 matrix. But still thanks, as I modified this a bit to get to the result. I actually need 2 for loops.
Massimo Zanetti
Massimo Zanetti 2017년 2월 24일
편집: Massimo Zanetti 2017년 2월 24일
The code above does EXACTLY what you need, it integrates every row by varying the row length and stroes everything in a 32x32 matrix B. You DON'T need two for loops.
Check carefully trapz help page.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by