필터 지우기
필터 지우기

Simple programming question about randomizing a vector.

조회 수: 2 (최근 30일)
Helen Kirby
Helen Kirby 2017년 1월 14일
댓글: Helen Kirby 2017년 1월 14일
clc
clear
ones = ones(1,20);
twos = ones+1;
thrs = twos+1;
fors = thrs+1;
fivs = fors+1;
Nums = [ones twos thrs fors fivs];
This should be dead easy but I cannot find a command to do it. Having got my blocks of 1s,2s,3s,4s,5s, in a single vector(Nums) I would now like to scramble them so that they are all mixed up. I tried "random" and "shuffle" but they gave me errors. Does anyone know how else I can randomize or shuffle up Nums?

채택된 답변

Image Analyst
Image Analyst 2017년 1월 14일
Try randperm():
% Get new indexes all scrambled up.
randomIndexes = randperm(length(Nums));
% Extract those indexes into a new vector.
scrambledNums = Nums(randomIndexes)
  댓글 수: 1
Helen Kirby
Helen Kirby 2017년 1월 14일
Yep, that did it! Thank you SO much - and so fast too. Very clever. HK.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by