Selecting one array out of three.

조회 수: 1 (최근 30일)
shane watson
shane watson 2017년 7월 20일
댓글: shane watson 2017년 7월 20일
I have three arrays, including A=[1 2 3 4 5 6 7 8 9 0 3 4 5 6]; B=[4 5 6 7 8 6 7 8 9 1 3 4 5 0]; C=[5 6 7 8 9 0 0 9 5 5 5 5 6 7]; Now I need to store one array in D i.e., D= any of one from three. Kindly help me out.

채택된 답변

Jan
Jan 2017년 7월 20일
편집: Jan 2017년 7월 20일
Or:
A = [1 2 3 4 5 6 7 8 9 0 3 4 5 6];
B = [4 5 6 7 8 6 7 8 9 1 3 4 5 0];
C = [5 6 7 8 9 0 0 9 5 5 5 5 6 7];
Pool = {A, B, C};
D = Pool{randi([1, numel(Pool)])}
This is flexible compared to the swicth method: The Pool can have different sized without the need to adjust the code.
  댓글 수: 1
shane watson
shane watson 2017년 7월 20일
Thank you, dear Jan Simon it Works nicely.

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

추가 답변 (2개)

Adam
Adam 2017년 7월 20일
n = randi( 3 )
switch ( n )
case 1
D = A;
case 2
D = B;
case 3
D = C;
end
If your 3 arrays where all in a single named variable as e.g. columns of a 2d array it would be neater, but not so much with named arrays.
  댓글 수: 1
shane watson
shane watson 2017년 7월 20일
Thank you, Adam, for your time. As it increases code length, but applicable in my work.

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


John BG
John BG 2017년 7월 20일
Hi Shane
the best way to randomly choose one of those vectors with variable length is to put them in a cell
A=[1 2 3 4 5 6 7 8 9 0 3 4 5 6];
B=[4 5 6 7 8 9 1 3 4 5 0];
C=[5 6 7 8 9 0 0 9 5 5 5 5 6 7];
D=randi([1 numel(A2)],1,1)
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  댓글 수: 1
shane watson
shane watson 2017년 7월 20일
Hi BG it gives an error since A2 is undefined for me??

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

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by