Reshape a row vector and convert it to a cell array
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a row vector as the following:
x = [1:10, 101:110, 11:20, 111:120]
Now I want to convert it to a 2-by-2 cell array C, where
C(1,1) = {[1:10]}
C(1,2) = {[11:20]}
C(2,1) = {[101:110]}
C(2,2) = {[111:120]}
How can I achieve this?
0 Commenti
Risposte (2)
Chunru
il 16 Nov 2021
x = [1:10, 101:110, 11:20, 111:120];
x = reshape(x, [20, 2])';
C = mat2cell(x, [1 1], [10 10])'
0 Commenti
Image Analyst
il 16 Nov 2021
Why do you want to do that?? That kind of situation does not call for a slow and inefficient cell array. You could simply use a 2-D double array:
C = reshape(x, [], 10);
Cell arrays are used for situations like where the arrays in each cell are not all of the same size. See the FAQ:
4 Commenti
Image Analyst
il 16 Nov 2021
@hmhuang, for that, try this:
x = [1:10, 101:110, 11:20, 111:120];
C = reshape(x, 10, [])';
Codd = C(1:2:end,:);
Ceven = C(2:2:end,:);
Codd = reshape(Codd', 20, [])';
Ceven = reshape(Ceven', 20, [])';
x = [Codd; Ceven]
Why do you need to do this rearrangement anyway?
Vedere anche
Categorie
Scopri di più su Antennas, Microphones, and Sonar Transducers in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!