summing logicals i times where i is taken from a matrix of integers

1 回表示 (過去 30 日間)
J T
J T 2016 年 9 月 9 日
編集済み: Andrei Bobrov 2016 年 9 月 9 日
I have a matrix of integers R
and some sort of critical value
crit = 0.395
depending on the value of any integer (i) in this matrix I want to generate a second random identically sized matrix of integers containing the sum of i components of the logical
crit >= rand
that is, if i = 0, it would return 0
if i = 1, crit >= rand, it would return either 0 or 1
if i = 2, it would sum crit >= rand + crit >= rand and would return either 0,1 or 2
Thus if R is
R = [1 2 1; 2 3 0; 0 1 0]
it might return
[1 1 0; 2 2 0; 0 0 0]
or
[0 2 1; 1 1 0; 0 1 0]
etc
In reality R is large so I have to avoid loops.
Any help appreciated!

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 9 月 9 日
編集済み: Andrei Bobrov 2016 年 9 月 9 日
EDIT
crit = 0.395;
z = max(R(:));
n0 = numel(R);
s = size(R);
Lo = crit >= rand([s,z]);
ii = reshape(1:n0,s);
out = arrayfun(@(jj)sum(Lo(jj+(0:R(jj)-1)*n0)),ii);
or just
out = arrayfun(@(ii)sum(rand(ii,1) <= crit),R)
  2 件のコメント
J T
J T 2016 年 9 月 9 日
Thanks for the reply and answer but it doesn't seem to address the randomisation I require from the expression
crit >= rand
which would need to be generated i times and then summed with i being the value of the integer for that particular element of the matrix.
Andrei Bobrov
Andrei Bobrov 2016 年 9 月 9 日
corrected code

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

その他の回答 (1 件)

J T
J T 2016 年 9 月 9 日
I think that's probably genius and I'll test it to make sure I get the right exponential recoveries in my Monte Carlo simulation
Many thanks!

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by