How to reshape and repeat at the same time.

Hi guys, I have the following data
data= [1 3 7 9 2 5]
and I want the data to be as a matrix (2*6)
newdata=1 1 7 7 2 2
3 3 9 9 5 5
Please help me, and thanks in advance.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Jan. 2017

0 Stimmen

num_rows = 2;
num_copies = 2;
new_data = kron(reshape(data, num_rows, []), ones(1, num_copies));
Or less verbosely,
new_data = kron(reshape(data,2,[]),ones(1,2));

5 Kommentare

Bahaa Algebory
Bahaa Algebory am 4 Jan. 2017
Thanks Walter, your answer is useful. Now, I want to convert this matrix to a vector as
finaldata= 1 1 3 3 7 7 9 9 2 2 5 5
Note: I will process the matrix (newdata), and at the end, I will convert this matrix to a vector.
kron(data, [1 1])
See also repelem if you have a newer MATLAB
Bahaa Algebory
Bahaa Algebory am 4 Jan. 2017
Bearbeitet: Walter Roberson am 5 Jan. 2017
Hi Walter, thanks again for your response. But actually, I want to convert the matrix to a vector. In other words, I will add a noise to the matrix and I will do other things. Then I want to convert the matrix to a vector.
the matrix is:
newdata= 1 1 7 7 2 2
3 3 9 9 5 5
I want to convert the above matrix to the following vector
finaldata= 1 1 3 3 7 7 9 9 2 2 5 5
finaldata = reshape( permute(reshape(new_data, 2, 2, []), [2 1 3]), 1, [] );
Bahaa Algebory
Bahaa Algebory am 5 Jan. 2017
Thanks Walter, your answer is awesome, and it is very useful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Roger Stafford
Roger Stafford am 2 Jan. 2017

0 Stimmen

newdata = reshape(repmat(reshape(data,2,[]),2,1),2,[]);

3 Kommentare

Bahaa Algebory
Bahaa Algebory am 4 Jan. 2017
Thanks Roger, your answer is useful. Now, I want to convert this matrix to a vector as
finaldata= 1 1 3 3 7 7 9 9 2 2 5 5
Note: I will process the matrix (newdata), and at the end, I will convert this matrix to a vector.
It is much easier to get ‘finaldata’ directly from ‘data’:
finaldata = reshape(repmat(data,2,1),1,[]);
Bahaa Algebory
Bahaa Algebory am 5 Jan. 2017
Bearbeitet: Bahaa Algebory am 5 Jan. 2017
Thanks Roger. You are right, it is easier to get "finaldata" directly from "data". But I do not want to do this. Actually, I have qpsk data, so I want to separate them into two streams. Then, I will add a noise. At the end, I will combine these two streams. So, that is why I want to get the "finaldata" from the matrix "newdata".
% newdata= 1 1 7 7 2 2
3 3 9 9 5 5
I want to convert the above matrix to the following vector
finaldata= 1 1 3 3 7 7 9 9 2 2 5 5
I hope you get my point.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by