Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

matrix

조회 수: 1 (최근 30일)
ricco
ricco 2011년 11월 7일
마감: MATLAB Answer Bot 2021년 8월 20일
I need to calculate the difference between values in a matrix without using the diff command. Is this done with a loop? I've tried:
for i=1:size(rho,2);
dz(:,i)=rho(:,i+1)-rho(:,i);
end
But obviously this wont work as the loop tries to continue beyond the the size of the matrix, how can this be done?
cheers

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 11월 7일
please read doc diff
e.g.
rho = randi(124,6)
dz = diff(rho,1,2) % in your case
  댓글 수: 2
ricco
ricco 2011년 11월 7일
diff works fine in obtaining what I need its just that the differences calculated are then used in an equation which includes the original values and the difference between the values. Where as the matrices are now of different dimensions they cannot be multiplied together. Is there a way of getting the matrices the same size? or is this just a case of adding an empty matrix in the begining to make up for it?
thanks
Walter Roberson
Walter Roberson 2011년 11월 7일
We cannot really advise you how to proceed without knowing more about the mathematics involved.
Specifying that you want differences between adjacent elements is inherently going to get you a smaller matrix, unless you define the difference carefully. For example, you can get an equal sized matrix if you define the difference as:
- absolute value of the forward difference for the left edge
- absolute value of the backward difference for the right edge
- maximum (or minimum) of the absolute value of the forward and backward difference for any interior point
But of course whether that makes any sense for what you are doing, we do not know.
If you tack on an extra row than you end up with a distortion along that edge. That could be important. Or not. We don't know.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by