index an array with arrays

10 ビュー (過去 30 日間)
Christian Hufnagel
Christian Hufnagel 2019 年 3 月 29 日
回答済み: James Tursa 2019 年 3 月 29 日
I have two index arrays i1 and i2 and a data array data.
I want to set the areas of data in the following way:
data([i1(1):i2(1), i1(2):i2(2)]) = ... % and so on
Is there a way to do that fast without loops? It can be achieved via
for i=1:length(d1)
data(d1(i):d2(i)) = 1.0;
end
  3 件のコメント
James Tursa
James Tursa 2019 年 3 月 29 日
How many dimensions does "data" have? Could that vary?
Christian Hufnagel
Christian Hufnagel 2019 年 3 月 29 日
I edited with an example. I want to build slices from d1 and d2
"data" is one-dimensional

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

採用された回答

James Tursa
James Tursa 2019 年 3 月 29 日
I'm still not exactly sure what you are trying to do, but If data is 1D and you want to set all values between the indexes to some common value as your example seems to indicate, then you could simply build an index vector to use. E.g.,
x = cell2mat(arrayfun( @(i1,i2)i1:i2, i1, i2, 'uni', false ));
data(x) = 1.0;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by