How to read data from a matrix according to the lower and upper index range

1 回表示 (過去 30 日間)
Given one logical matrix a, b defines the lower and upper index range of each row of a to be 1. May I know how to vectorize the following code, as the loop is huge (1,000,000 loops) for the real case?
a=zeros(5,24,'logical');
b=[2 4;5 7;8 9;1 10;12 15];
for i=1:5
a(i,b(i,1):b(i,2))=1;
end

採用された回答

sloppydisk
sloppydisk 2018 年 5 月 1 日
Define a grid to perform logical indexing on:
n = 24;
m = 5;
gridx = repmat(1:n, m, 1);
b=[2 4;5 7;8 9;1 10;12 15];
a = gridx >= b(:, 1) & gridx <= b(:, 2);
That should do the trick.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by