How to change values in a 2 dimensional vector at random points

Lets say I have n*m matrix. For each row I would pick a random starting point and change the values from that point to the end of the row.
x=ones(n,m);
T=randsample(m,n); %starting points for each row
End=m*ones(size(T)); %ending points for each row
x(:,T:End)=0 %This doesn't work but something like that
Eg:
magic(5);
T=randsample(5,5);
T =
2
3
1
5
4
%Now I want to make every value from T to end in X=0
for i=1:5
x(i,T(i):end)=0
end
Is there a way to vectorize this. If I'm simulating a million trials using a for loop is not very efficient.

 採用された回答

David Young
David Young 2012 年 3 月 7 日

0 投票

This might use too much memory, but it's worth a try:
c = meshgrid(1:m, 1:n); % size(x) is [n m], not [m n]
change = bsxfun(@gt, c, T);
x(change) = newVal;

1 件のコメント

Lakshmi Kuchipudi
Lakshmi Kuchipudi 2012 年 3 月 8 日
It served my purpose. Thank you very much. :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by