Reduce one for loop?

1 次查看(过去 30 天)
Hello, I have a bunch of code in which there are 2 for loops. I want to increase the speed and have just one for loop. here is the lines:
for i=1:length(bid)
for j=1:pidlastindex
if(bid(i)==pid(j))
C(i) = delay(j);
end
j=j+1;
end
i=i+1;
end
To do so I did :
for i=length (bid)
C(i)=delay(bid==pid(i))
end
But I get an error: In an assignment A(:)=:B, the number of elements in A and B must be the same.
Could any one tell me what is wrong here?
Also C in that loop changes the dimension on every loop iteration.
Thanks.

采纳的回答

Walter Roberson
Walter Roberson 2016-2-11
What if there are multiple locations that match the bid? What if there are no locations that match the bid? What if one of the values is 23.45 and the other value is 23.45+(4e-15) and you can't tell the difference because they both show up as 23.45 on the screen?
  2 个评论
Farzaneh ziaee
Farzaneh ziaee 2016-2-11
Ok good comment! Actually my bid vector is larger than pid and each value has some repetitions and pid is defined to get the unique values of bid. now I want to check if the row lets say number one in bid matches the first row of pid matlab gets the first row of the third vector which is delay and store it in a vector
Walter Roberson
Walter Roberson 2016-2-12
If pid is the unique values in bid then bid==pid(i) can be true in multiple locations in bid. That would select multiple delay, and you cannot store multiple values into the single destination C(i).
Perhaps you want something like
delay(find(bid==pid(i), 1, 'first'))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 PID Controller Tuning 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by