MATLAB Answers

how to 7004829X1 into 5000X1 blocks?

2 views (last 30 days)
I want to separate 1400 blocks and they receive 10 data from the previous and next matrices

  2 Comments

TADA
TADA on 19 Dec 2018
Could You Publish An Example Of The Input And Desired Output?
Cem SARIKAYA
Cem SARIKAYA on 19 Dec 2018
For example, if I have matrix: A = [1,2,3,4,5,6,7,8,9,10,11,12,13];
and I want matrices:
B = [1,2,3,4,5]; C = [4,5,6,7,8]; D=[8,9,10,11,12];
but i have 7000000x1 matrix and i need 1400 pieces matrix

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 19 Dec 2018
You say "they receive 10 data from the previous and next matrices" so you want an overlap of 10 when the window jumps to the next location.
You can use blockproc() if you have the Image Processing Toolbox to process those blocks with jumps of whatever you want and a window size of whatever you want. For example a window size of 1400 but the window jumps 1390 each time, instead of 1400, so that you have an overlap of 10 in each window. See attached example and adapt as needed.

  5 Comments

Show 2 older comments
Cem SARIKAYA
Cem SARIKAYA on 19 Dec 2018
this is what i need but this code overwrites all blocks in thisBlock would you change it?
Image Analyst
Image Analyst on 19 Dec 2018
Why do you need to save them? Is there some reason you need them after the loop exits? Why can't you just process them right there in the loop?
If for some reason you need the blocks later, after the loop is done, then you can save them to a cell array. You could save them to a 2-D numerical array but since the last block is only 3859 elements long, you can't do that (unless you wanted to pad the rest with some number).
thisBlock{k} = data(index1:index2);
Cem SARIKAYA
Cem SARIKAYA on 20 Dec 2018
thank you very much you correct my perspective. I appreciate.

Sign in to comment.

More Answers (2)

Mark Sherstan
Mark Sherstan on 19 Dec 2018
Edited: Mark Sherstan on 19 Dec 2018
Try this (you will have to adjust for your larger vector) but should do the trick:
A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
count = 1;
for ii = 3:3:length(A)-3
idxLow = ii-2;
idxHigh = ii+2;
out{count} = A(idxLow:idxHigh)';
count = count + 1;
end

  4 Comments

Show 1 older comment
Cem SARIKAYA
Cem SARIKAYA on 19 Dec 2018
i want;
{5000x1 double}
{5000x1 double}
{5000x1 double}
{5000x1 double}
{5000x1 double}
{5000x1 double}
.
.
etc
Mark Sherstan
Mark Sherstan on 19 Dec 2018
Sorry I missed a counter. Correction was made in the original post and located below for your convenience.
A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
count = 1;
for ii = 3:3:length(A)-3
idxLow = ii-2;
idxHigh = ii+2;
out{count} = A(idxLow:idxHigh)';
count = count + 1;
end
out =
1×4 cell array
{5×1 double} {5×1 double} {5×1 double} {5×1 double}
Cem SARIKAYA
Cem SARIKAYA on 19 Dec 2018
thank you very much but, how can I change it to 5000 values

Sign in to comment.


TADA
TADA on 19 Dec 2018
Edited: TADA on 19 Dec 2018
You can either generate a matrix with each column representing one 1400 part vector as you specified, or generate a cell array, with each cell containing that vector.
a = (1:15)';
% generate a matrix of 5x3
b = reshape(a, [5,3])
b =
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15
% generate a cell array of 3 cells with 5x1 vector in each cell
c = mat2cell(a, repmat(5, 1, 3), 1);
celldisp(c);
c{1} =
1
2
3
4
5
c{2} =
6
7
8
9
10
c{3} =
11
12
13
14
15
as for the large vector you have:
a = (1:7000000)';
b = reshape(a, [1400, 5000]);
c = mat2cell(a, repmat(1400, 1, 5000), 1);

  5 Comments

Show 2 older comments
Cem SARIKAYA
Cem SARIKAYA on 19 Dec 2018
thank you very much but i need like this For example, if I have matrix: A = [1,2,3,4,5,6,7,8,9,10,11,12,13];
and I want matrices:
B = [1,2,3,4,5]; C = [4,5,6,7,8]; D=[8,9,10,11,12];
Image Analyst
Image Analyst on 19 Dec 2018
That's an example of overlap of 1. You can use blockproc() if you have the Image Processing Toolbox to process those blocks with jumps of whatever you want and a window size of whatever you want. For example a window size of 1400 but the window jumps 1390 each time, instead of 1400 so that you have an overlap of 10 in each window. See attached example in my Answer.
TADA
TADA on 19 Dec 2018
Thanks Image Analyst for pointing out my mistake.
Here's the rectified version:
% input
a = (1:13);
% settings
overlap = 2;
n = 5;
% generate the indices of column starts
i = 1:(n-overlap):(length(a)-n);
% generate the output matrix
x = bsxfun(@plus, i, (0:(n-1))');
x =
1 4 7
2 5 8
3 6 9
4 7 10
5 8 11

Sign in to comment.

Sign in to answer this question.

Tags