6
我有一個持有「開始」和「結束」位置(如邏輯值)兩個向量,我想作爲結合創造出第三個向量,Final
:創建矢量範圍從變量
Starts = [0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0];
Ends = [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0];
隨着最終的載體看起來像這樣:
Final = [0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 0];
目前,我可以使用for
循環如下做到這一點:
Start_Locations = find(Starts);
End_Locations = find(Ends);
Final = zeros(20,1);
for x=1:length(Start_Locations)
Final(Start_Locations(x):End_Locations(x),1) = 1;
end
我想知道是否有一種方法可以在沒有for
循環的情況下完成同樣的事情。例如,我可以完成我用下面的「硬編碼」的聲明上述:
Final([4:8,11:19],1) = 1;
具體而言,是有辦法的Start_Locations
和End_Locations
向量的方式結合起來,這樣我可以有一個單一的像這樣的語句:
Final(Combined_Start_and_End_Locations,1) = 1;
要完成我在for
循環上面做了什麼?我試圖儘可能地避免for
循環,並且非常感謝任何解決方案,如上所述創建Final
向量,而不訴諸於循環。
在這種情況下,這個解決方案返回'[0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1]'這對我來說似乎是正確的。 – Trogdor 2014-09-23 20:00:12
是的你是對的,我想我還想別的東西! +1 – Divakar 2014-09-23 20:01:18
喔,你讓我擔心了一分鐘......謝謝 – Trogdor 2014-09-23 20:04:51