필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Fill matrix randomely without row-repetitions

조회 수: 2 (최근 30일)
JB
JB 2017년 6월 5일
마감: MATLAB Answer Bot 2021년 8월 20일
Please help. I'm new to matlab scripting and need a bit of help. I have a series of numbers:
A=[1 1 1 2 2 2 3 3 3 4 4 4 5]
which I wants to fill randomely into a 8x12 matrix without having the same number in the same row. At the end I want all the "empty" cells of the 8x12 matrix being filled with 0's or nan.
An example could be:
result=
3 1 5 2 4 5 0 0 0 0 0 0
4 1 3 2 0 0 0 0 0 0 0 0
1 3 4 2 0 0 0 0 0 0 0 0

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 6월 6일
A = [1 1 1 2 2 2 3 3 3 4 4 4 5];
result = nan(8,12);
result(1:numel(A)) = A;
[~,ii] = sort(rand(8,12),2);
result = result(bsxfun(@plus,(ii - 1)*8,(1:8)'));
result = result(randperm(8),:)

이 질문은 마감되었습니다.

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by