how to creat a 0and 1 matrix by another matrix

2 ビュー (過去 30 日間)
Junw
Junw 2014 年 6 月 3 日
回答済み: Niklas Nylén 2014 年 6 月 3 日
example: I have one random number matrix a. a=[2 3 4 5 ],and then I want to use a matrix to create another matrix b depending on a.
b=[1 1 0 0 0 1 1 1 1 0 0 0 0 0].The elements in a is to describe the number of 0 and 1 in matrix b.
Please don't use for loop,it's too slow.

回答 (1 件)

Niklas Nylén
Niklas Nylén 2014 年 6 月 3 日
"Please don't use for loop,it's too slow." In which context are you using the function if the following code is too slow?
If a has length 10000 this code will run in 0.009 s on my computer:
tic
b = ones(1,sum(a));
acumsum = cumsum(a);
for ii = 2:2:length(a)
b(acumsum(ii-1)+1:acumsum(ii))=0;
end
toc

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by