matrix array

조회 수: 9 (최근 30일)
Tom Fredrick
Tom Fredrick 2011년 10월 4일
Please can someone be of help? Here is my code below:
a=[2 2 2 2 4 4 4 4];
b = [0 0 0 0 1 1 1 1];
c = Shuffle(b);
d = c(a)
When I finished inputting the code, d returned 8 zeros. Instead I want after randomizing c, then d will return two (zeros) and two(ones) that tally with the four (2's) and four (4's)in the first variable a. In other words,I want d to return two zeros and two ones for the 2's and the same also for the 4's regardless of the order. In essence I want d to return something like this: d = 0 1 0 1 1 0 0 1 OR 0 0 1 1 0 1 1 0. Thanks in advance, Tom.
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
what is shuffle()?
Tom Fredrick
Tom Fredrick 2011년 10월 4일
shuffle rearranges the elements in a random order.

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

답변 (2개)

the cyclist
the cyclist 2011년 10월 4일
I don't really understand what you want. But I think you can probably make good use of the randperm() command.

Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
Okay, shuffle() is your own function. I can do that using randperm() like cyclist suggested. It's basically the same.
a=[2 2 2 2 4 4 4 4];
b = [0 0 0 0 1 1 1 1];
c = b(randperm(length(b)));
d = c(a)
Since a is [2 2 2 2 4 4 4 4], so d will be the 2nd and 4th value of c, each repeats 4 times. So d has 4 possible answer.
  1. all zeros
  2. all ones
  3. four zeros and then four ones
  4. four ones and then four zeros
Please double check your logic to see if you missed anything.
Considering your expected output, maybe all you need is:
d=b(randperm(length(b)))
  댓글 수: 1
Tom Fredrick
Tom Fredrick 2011년 10월 6일
Thanks Fangjun.
Tom

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by