How to expand a Matrix ?

142 views (last 30 days)
Jerin Manjooran Varghese
Jerin Manjooran Varghese on 24 Sep 2018
Commented: alper cicek on 19 Jan 2021
I need to expand a matrix of size 505*3 to a size of 30300*3. Basically 30300/505 is 60. My requirement is to copy the same values of every row to the next 59 rows and thereby increase the matrix size. The whole idea is to convert the values of some data we receive hourly to mins. The data changes hourly however the computation is required to be in mins.Thus the values would remain the same for every hour. Any help would be appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Sep 2018
NewMatrix = kron(YourMatrix, ones(60,1));
You should also consider using retime()
  3 Comments
John D'Errico
John D'Errico on 24 Sep 2018
Yes, it is.
alper cicek
alper cicek on 19 Jan 2021
Thanks :)

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 24 Sep 2018
Edited: John D'Errico on 24 Sep 2018
One answer is to just use indexing, using a carefully designed index vector. Or, you can use tools like repmat & permute & reshape, and think about how data is stored in memory. Both work...
A = rand(505,3);
indexing:
ind = ceil((1:(505*60))/60);
B = A(ind,:);
If you want to understand why it works, look at the vector ind. Think about how it was created.
repmat:
B = reshape(permute(repmat(A,[1 1 60]),[3 1 2]),505*60,3);
To understand this one, you need to take apart each piece. First, think about what the repmat does. Then look at permute. Understand what that does in memory. Finally, look at how reshape works. So, effectively, look at what each of these steps do:
B1 = repmat(A,[1 1 60]);
B2 = permute(B1,[3 1 2]);
B = reshape(B2,505*60,3);
Read the help for these things if you don't understand them. This is the only way you will learn MATLAB.

Stephan
Stephan on 24 Sep 2018
Edited: Stephan on 24 Sep 2018
Hi, this would work i think:
A = [1 2 3; 4 5 6]
n = 3 % Number of repeated values --> 60 for your case
for k = 1:size(A,2)
B(:,n*k-(n-1) : n*k) = repmat(A(:,k),1,n);
end
For example:
A =
1 2 3
4 5 6
n =
3
B =
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
Maybe there are more elegant ways to do this.
Best regards
Stephan
  1 Comment
John D'Errico
John D'Errico on 24 Sep 2018
While an idea like this can work, be careful. If you do it without preallocation, it can be terribly slow when you start creating large arrays.
Otherwise, MATLAB will need to reallocate memory each time, for a slightly larger array. Not a problem for a matrix that ends up growing only 3 times. But what if you wanted to do it 60 times? What if you wanted to replicate it 3600 times to get data by the second?
This is why you preallocate such arrays. If B is already the correct size in advance, then MATLAB only needs to stuff data into the proper place in memory. Without preallocation, you have an array that grows, taking quadratically increasing time. Again, no real problem if you do it 3 times,
If you want to see the difference, test the time for different amounts of replication, comparing what happens. This is perhaps the number one reason why people ask questions about how to speed up their code on Answers, and it is the easiest problem to fix. Just preallocate B in advance.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!