using 2 indexs for loop

조회 수: 2 (최근 30일)
Morten Jørgensen
Morten Jørgensen 2019년 3월 21일
댓글: Morten Jørgensen 2019년 3월 22일
Hi
how can i optimize this loop, if i want to include another indexing
mHome is an 15001x28 array
mBall is an 15001x3 array
for i=1:length(mHome)
output1(i,1)=pdist([mHome(i,1:2);mBall(i,1:2)]);
output1(i,2)=pdist([mHome(i,3:4);mBall(i,1:2)]);
output1(i,3)=pdist([mHome(i,5:6);mBall(i,1:2)]);
output1(i,4)=pdist([mHome(i,7:8);mBall(i,1:2)]);
output1(i,5)=pdist([mHome(i,9:10);mBall(i,1:2)]);
output1(i,6)=pdist([mHome(i,11:12);mBall(i,1:2)]);
output1(i,7)=pdist([mHome(i,13:14);mBall(i,1:2)]);
output1(i,8)=pdist([mHome(i,15:16);mBall(i,1:2)]);
output1(i,9)=pdist([mHome(i,17:18);mBall(i,1:2)]);
output1(i,10)=pdist([mHome(i,19:20);mBall(i,1:2)]);
output1(i,11)=pdist([mHome(i,21:22);mBall(i,1:2)]);
output1(i,12)=pdist([mHome(i,23:24);mBall(i,1:2)]);
output1(i,13)=pdist([mHome(i,25:26);mBall(i,1:2)]);
output1(i,14)=pdist([mHome(i,27:28);mBall(i,1:2)]);
end
% I have tried this but it does not work
for i=1:length(mHome)
for k=1:size(mHome,2)
output1(i,k)=pdist([mHome(i,k);mBall(i,1:2)]);
end
end
  댓글 수: 3
madhan ravi
madhan ravi 2019년 3월 21일
Even after the below?
for k=1:size(mHome,2)
output1(k,1)=pdist([mHome(k,(k*2)-1:k*2);mBall(k,1:2)]);
end
What the size of mBall?
Morten Jørgensen
Morten Jørgensen 2019년 3월 21일
mBall is a 15001x3 array.
but im only using the first 2 columms
my output should be a 15001x14 array

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

채택된 답변

Thorsten
Thorsten 2019년 3월 21일
for i=1:length(mHome)
for k=1:2:size(mHome,2)-1
output1(i,k)=pdist([mHome(i,k:k+1);mBall(i,1:2)]);
end
end
  댓글 수: 1
Morten Jørgensen
Morten Jørgensen 2019년 3월 22일
many thanks.
the only problem is that the output is a 15001x27 array and it should be a 15001x27 array.
right now everyother column is a colum with 0

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by