For loop problem.

조회 수: 2 (최근 30일)
Darkhan Kenestegi
Darkhan Kenestegi 2017년 1월 30일
편집: Stephen23 2017년 1월 30일
I am trying to loop my calculations with a long data, but I do something wrong.
size(a)=15 186
for i=1:size(a,1)
bcn(i,:)=abs(a(i,:)-a(i+1,:));
end
The idea is that bcn should equal like that bcn=abs(a(1,1)-a(2,1))+abs(a(1,2)-a(2,2))+abs(a(1,3)-a(2,3))+abs(a(1,4)-a(2,4))+...+abs(a(i,:)-a(i+1),:);
As you can see this is quite extensive procedure. I know that my first column only have 15 entries. But is there any trick to acquire those numbers anyway. Clearly I am interested in the differences between the data.
Cheers! Will appreciate any help!
  댓글 수: 1
Darkhan Kenestegi
Darkhan Kenestegi 2017년 1월 30일
I actually get the answer anyway. So I kinda already solved. But now I need to know how I can improve the code so I can make difference with any row, not just consequential...

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

답변 (1개)

Stephen23
Stephen23 2017년 1월 30일
편집: Stephen23 2017년 1월 30일
abs(diff(a,1))
and then play around with sum. For example:
>> A = randi(9,5,10)
A =
8 1 7 6 2 3 1 9 1 8
7 4 9 3 6 2 2 5 2 1
5 8 3 5 6 9 9 8 9 9
9 4 9 6 2 3 9 3 5 4
4 2 1 2 2 6 3 3 5 8
>> B = abs(diff(A,1))
B =
1 3 2 3 4 1 1 4 1 7
2 4 6 2 0 7 7 3 7 8
4 4 6 1 4 6 0 5 4 5
5 2 8 4 0 3 6 0 0 4
>> sum(B,1)
ans =
12 13 22 10 8 17 14 12 12 24
>> sum(B,2)
ans =
27
46
39
32
  댓글 수: 3
Darkhan Kenestegi
Darkhan Kenestegi 2017년 1월 30일
Changing numbers in diff(A,1) not really does what I wanted. So diff(A,2) takes the difference of the difference, which is not really an aim, although it is interesting.
diff(A,1) calculates difference only between adjacent rows.
Stephen23
Stephen23 2017년 1월 30일
편집: Stephen23 2017년 1월 30일
abs(a(1,1)-a(2,1))+abs(a(1,2)-a(2,2))+abs(a(1,3)-a(2,3))+abs(a(1,4)-a(2,4))+...
could be written like this:
sum(abs(diff(A,1)),2)

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by