# rearrangement rows of matrices

8 visualizzazioni (ultimi 30 giorni)
parham kianian il 17 Nov 2020
Commentato: Rik il 2 Feb 2022
Suppose:
A = repmat((1:15)',1,5);
Now I want to rearrange A such that it becomes matrix of size 5 by 15 in following form:
B = [A(1:5,:), A(6:10,:), A(11:15,:)];
The problem is that size of matrix A in my work is too big and its size may change in each iteration. So I can't use a fix command like above to evaluate B. On the other hand, using a for loop is too much time consuming. I tried function "reshape". But, it does not work well.
Is there any function to do this? or how should I call function reshape to get what I want?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Bruno Luong il 17 Nov 2020
A = reshape(A,[5 3 5]);
A = permute(A,[1 3 2]);
A = reshape(A,[5 15]);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (3)

Rik il 17 Nov 2020
The easiest way is probably to use a cell as an intermediary step:
A = repmat((1:15)',1,5);
B = [A(1:5,:), A(6:10,:), A(11:15,:)];
d=5;%or is this size(A,2)?
C=mat2cell(A,...
d*ones(1,size(A,1)/d),...
size(A,2));
C=C.';
C=cell2mat(C);
isequal(B,C)
ans = logical
1
This would all be much simpler if your example array is actually this repetitive, as you could use repelem.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

KSSV il 17 Nov 2020
A = repmat((1:15)',1,5);
B = [A(1:5,:), A(6:10,:), A(11:15,:)];
[r,c] = size(A) ;
n = 3 ;
C = permute(reshape(A',[c,r/n,n]),[2,1,3]);
D = reshape(C,5,[]) ;
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Andrei Bobrov il 17 Nov 2020
A = reshape(1:75,15,[]);
a = 5;
m = size(A,1);
B = reshape(permute(reshape(A,a,m/a,[]),[1,3,2]),a,[]);
##### 2 CommentiMostra NessunoNascondi Nessuno
Antonio Carvalho il 2 Feb 2022
@Andrei Bobrov how are you. Can i reshape two long vectors? one has a lenght completely different to another and from a spefic point (time) compare them using a plot figure?
Rik il 2 Feb 2022
You can't. If you don't have as many x-values as you have y-values it is not possible to match them up and create a plot. You need to make sure you can make pairs of your x-y-values.

Accedi per commentare.

### Categorie

Scopri di più su Sparse Matrices 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!

Translated by