필터 지우기
필터 지우기

Setting value to zero at random

조회 수: 2 (최근 30일)
Ulrik Nash
Ulrik Nash 2011년 7월 9일
Hi Everyone,
Suppose I have two vectors
A = [0 2 2 0 1]
and
B = [1 3 4 2 1]
and suppose I apply following code:
val = max(nonzeros(A)); [~,J] = find(A == val); z(1,J) = 0;
Then the result is to change the 3 and the 4 in B to 0.
My problem is simply, that I only wish to change one place in B to 0, and the place actually changed should be chosen completely at random from those max nonzeros.
How could I adjust my code to allow this?
Regards,
Ulrik.

채택된 답변

Paulo Silva
Paulo Silva 2011년 7월 9일
idx=find(max(nonzeros(A))==A);
B(randi([idx(1) idx(end)]))=0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by