How to repeat the matrix to complete the shape of Matrix
Show older comments
Hello everyone i hope you are doing well.
I have the following Dataset,
one has shape of 1x586 and other has shape 1x48.
I want to repeat this matrix to to complete the shape of 1x1000 and 1x10000.
How can i do this in MATLAB
1 Comment
Rik
on 11 Apr 2022
What have you tried so far?
Also, 48 and 586 are not integer fractions of 1e3 and 1e4. How do you want to deal with that?
Answers (1)
Walter Roberson
on 11 Apr 2022
Your requirements are not clear.
targetsize586 = 10000;
n586 = numel(Mat_586);
nrep = targetsize586 / n586;
fullrep = floor(nrep);
leftover = targetsize - fullrep * n586;
variation1 = [repmat(Mat_586, 1, fullrep), Mat_586(1:leftover)];
variation2 = repelem(Mat_586, 1, ceil(nrep)); variation2(targersize586+1:end) = [];
variation1 makes repeated copies of the matrix, like [1 2 3, 1 2 3, 1 2 3, 1 2 3, ...] as many times as possible to fit within the target size, and then makes a partial copy as large as needed to fit.
variation2 makes repeated copies of the elements, like [1 1 1 1, 2 2 2 2, 3 3 3 3, ...] with each element repeated as many times as needed until the overall would meet or exceed the target size, and then trims off enough copies of the last element to make the overall fit. So you might, for example, get 21 copies in a row of each value except the last, followed by (say) 18 copies of the last element.
8 Comments
Stephen john
on 11 Apr 2022
Walter Roberson
on 11 Apr 2022
Go on to the next row of what? Check if 1000 samples are present in what?
Stephen john
on 11 Apr 2022
Walter Roberson
on 11 Apr 2022
Edited: Walter Roberson
on 11 Apr 2022
Okay, the 1176 example. When filling in the second row, should you start back from the beginning of the 1176, or should you only start from the part after 1000?
[m1, m2, ... m1000
m1001, m1002, ... m1176, m1, m2, ... m824]
or
[m1, m2, ... m1000
m1001, m1002, ... m1176, m1001, m1002, ... m1176, m1001, m1002, ...]
Stephen john
on 11 Apr 2022
Walter Roberson
on 11 Apr 2022
Edited: Walter Roberson
on 11 Apr 2022
Yes, that is not the question. The question is what happens when you need to start repeating values to fill up the second row.
Notice in the first possibility, after the last of the original entries, m1176, I start repeating from the first over-all entry, m1.
Notice in the second possibility, after the last of the original entries, m1176, I start repeating from the first value that is on this same row, m1001.
It is not clear which of these two possibilities you are after?
Suppose the input data was exactly 1001 elements long. Then should the output be
[m(1:1000);
m(1001), m(1:999)]
or should it be
[m(1:1000);
repmat(m(1001), 1, 1000)]
Stephen john
on 11 Apr 2022
Walter Roberson
on 11 Apr 2022
targetsize = 10000;
output = [];
temp = reshape(YourData, 1, []); %ensure row
nt = numel(temp);
while nt >= targetsize
output = [output; temp(1:targetsize)];
temp = temp(targetsize+1:end);
nt = numel(temp);
end
nrep = ceil(targetsize / nt); %we know nt < targetsize at this point
temp = repmat(temp, 1, nrep);
output = [output; temp(1:targetsize)];
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!