フィルターのクリア

Set of indexes to vector without loop?

3 ビュー (過去 30 日間)
Kamil Antos
Kamil Antos 2017 年 2 月 17 日
編集済み: Guillaume 2017 年 2 月 17 日
Hi, the problem is as follow:
There is matrix with indexes:
IND =
1 15
26 40
51 65
I would like to create vector which looks like:
VEC = [1,2,...14,15,26,27,...39,40,51,52...64,65]
For that specific example answer is:
VEC = [1:15, 26:40, 51:65]
but I would like to find general solution without looping. It should be independent from number of rows in in IND.
I didn't find any solution in Matlab answers but I am sure that is possible.
Any ideas? Best Kamil

採用された回答

Guillaume
Guillaume 2017 年 2 月 17 日
編集済み: Guillaume 2017 年 2 月 17 日
A for loop as per KSSV's answer is probably the best way to do this.
cell2mat(arrayfun(@(s,e) s:e, IND(:, 1), IND(:, 2), 'UniformOutput', false)')
would be one way to do it without a loop, if you consider arrayfun not to be a loop. It's more likely to be slower than an actual for loop.
See also my old cody problem which asks to do just the same. For information, the best scoring solution is:
str2num(sprintf('%d:%d ', IND'))
whose only merit is that it is low scoring on cody. Certainly don't use that for real code, it's going to be very slow.

その他の回答 (1 件)

KSSV
KSSV 2017 年 2 月 17 日
iwant = [] ;
for i = 1:size(IND,1)
iwant = [iwant IND(i,1):IND(i,2)] ;
end
A more elegant solution might be possible.
  2 件のコメント
Kamil Antos
Kamil Antos 2017 年 2 月 17 日
Hi KSSV, thank you for answer. This is what I use so far. I would like to get rid of loop.
KSSV
KSSV 2017 年 2 月 17 日

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by