Creating different types of arrays with conditions, then make an array with the combination of all elements, and finally separate the arrays with a sequence

1 view (last 30 days)
Suppose,
x and y are two variables. I want to make x number of arrays each containing y elements where each array elements will be multiplied by (1000*x). Then, I want to make an array (say A) with the combination of the x array elements.
For example, x=3 and y=4, then there will be three arays (say a1, a2, a3) each will contain y elements i.e. [1 2 3 4] multiplied by (1000*x).
i.e., for
x=1, a1=[1001 1002 1003 1004]
x=2, a2=[2001 2002 2003 2004]
x=3, a3=[3001 3002 3003 3004]
Now, the array (A) will be the combination of a1, a2 and a3; for instance
A=[1001 2003 2004 3001 3004 1002 2001 2002 1004 1003 3002 3003 ]
(Q1) How can I construct the array A easily?
Later, I want to sequence a1, a2 and a3 based on the sequence of their elements in A.
i.e.,
a1=[1001 1002 1004 1003]
a2=[2003 2004 2001 2002]
a3=[3001 3004 3002 3003]
(Q2) How can I construct a1, a2, a3 from the array A easily?

Accepted Answer

Voss
Voss on 18 Jan 2022
x = 3;
y = 4;
a = 1000*(1:x).'+(1:y)
a = 3×4
1001 1002 1003 1004 2001 2002 2003 2004 3001 3002 3003 3004
A = a(randperm(x*y))
A = 1×12
2003 3003 3001 1002 2001 2002 1001 3004 2004 1003 3002 1004
[~,idx] = ismember(A,a);
[~,new_idx] = sort(mod(idx-1,x)+1);
a_new = reshape(a(idx(new_idx)),[],x).'
a_new = 3×4
1002 1001 1003 1004 2003 2001 2002 2004 3003 3001 3004 3002

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!