Randomly assign a value to each column of a matrix

조회 수: 2 (최근 30일)
Peter Harrington
Peter Harrington 2019년 6월 17일
편집: dpb 2019년 6월 18일
I can't figure out why this code doesn't work. I would like to have one true value in each row that is randomly assigned to a column like so
[1 0 0;
0 1 0;
1 0 1;
0 0 1]
mt = 5;
npart = 3;
seqS = false(mt, npart);
t = randi([1, 3], mt, 1);
seqS(:, t) = true; %should work, why not?
Thanks in advance,
Pete

채택된 답변

dpb
dpb 2019년 6월 18일
Not way subscript expansion works as written...use
seqS(sub2ind(size(seqS),[1:mt].',t)) = true;
instead
  댓글 수: 2
Peter Harrington
Peter Harrington 2019년 6월 18일
Thanks for the help, your code works great!
dpb
dpb 2019년 6월 18일
편집: dpb 2019년 6월 18일
No problem, glad to help...it's one of those syntax things that superficially does look like should work as you expected but doesn't.
If would, go ahead and Accept answer if for no other reason to indicate to others there is a solution to the question. (And, while I never started this with the intent of garnering points, as of this instant, you'd be the one to hit the 10K mark... :) )

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by