Vecotrizing a for loop

5 ビュー (過去 30 日間)
hemant
hemant 2016 年 11 月 2 日
回答済み: Adam 2016 年 11 月 2 日
I have been trying to write more efficient piece of code and one obvious aspect of doing that is vectorization.I know the basic stuff about that but here's where I can't figure out. So, I have a cell array "dataCell" in which each cell contains a single vector. The length of the vector varies across the cells. Now, I wish to put all the vectors in a single vector named "dataRow" and to make use of vectorisation, I have two another vectors "startIndex" and "numberOfElements" that give the first and the last index for each vector in dataCell that it should occupy in the vector "dataRow". Here is the for loop version of what I wish to do,
for i=1:length(dataCell)
dataRow(startIndex(i)+1:startIndex(i)+numberOfElements(i))=dataCell{i}
end
Can anyone suggest anything? Thanks!
  2 件のコメント
Adam
Adam 2016 年 11 月 2 日
編集済み: Adam 2016 年 11 月 2 日
Are these start indices different from the order of the cells in some way?
If not then simply
dataRow = [ dataCell{:} ]
should work.
hemant
hemant 2016 年 11 月 2 日
You're right! Thanks

サインインしてコメントする。

採用された回答

Adam
Adam 2016 年 11 月 2 日
dataRow = [ dataCell{:} ]

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by