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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Md. Asadujjaman
am 18 Jan. 2022
Beantwortet: Voss
am 18 Jan. 2022
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?
0 Kommentare
Akzeptierte Antwort
Voss
am 18 Jan. 2022
x = 3;
y = 4;
a = 1000*(1:x).'+(1:y)
A = a(randperm(x*y))
[~,idx] = ismember(A,a);
[~,new_idx] = sort(mod(idx-1,x)+1);
a_new = reshape(a(idx(new_idx)),[],x).'
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!