Create an Array of Vectors from Existing Vector

12 views (last 30 days)
P.
P. on 12 Nov 2014
Edited: Star Strider on 12 Nov 2014
This is a silly question, but I am terrible with Matlab, which limits my usefulness in searching for help online. Any help would be greatly appreciated.
I have a vector that is 83000x1. I am trying to break it into 41 different vectors, each 2000x1. How can I create an array that has all of the data in it? I'd like it create a new v(i) for each iteration, that v being a vector of all of the data for that data set( i = 1, v = 1-2000)
This is what I've been trying to work with...
A = xlsread('Lab_A05_A_XD_15','B127:B83126');
for i = 1:41
b = 1+i*2000-2000;
c = 2000*i;
v{i} = {A(b):A(c)};
end
Thanks!

Answers (1)

Star Strider
Star Strider on 12 Nov 2014
The reshape function is your friend here.
If ‘V’ is your (83000x1) column vector, ‘M’ will be your data matrix that results from reshaping it:
V = [1:83000]';
N = 41*2000;
M = reshape(V(1:N),2000,[]);
  2 Comments
P.
P. on 12 Nov 2014
You're the man Star, thank you.
This presents a new issue though.. there's empty cells between each data set (25 cells) that are now throwing off the matrix. The first column is fine, the second is offset by 25 'NaN' that push data into the next column, with another 25 'NaN' be added each column. Do you have any quick ideas?
Star Strider
Star Strider on 12 Nov 2014
Edited: Star Strider on 12 Nov 2014
My pleasure!
We need to know how many NaN values are in your data, and where they are to make this work. Also, are all your data 2000 elements long, or do they vary in length?
For the NaN problem, do this (again assuming ‘V’ is your vector, so make the appropriate substitution in the ‘Vdat’ and ‘Vnan’ assignments):
Vdat = find(~isnan(V)); % Find Data
dVdat = diff([0; Vdat]);
sVdat = [1; find(dVdat>1)];
pVdat = Vdat(sVdat); % Data Start Indices
Vnan = find(isnan(V)); % Find NaNs
dVnan = diff([0; Vnan]);
sVnan = find(dVnan>1);
pVnan = Vnan(sVnan); % NaN Start Indices
nVnan = size(Vnan); % Number of NaNs
This should tell us what we need to know to reshape your array, so please send along the results of: ‘pVdat’, ‘pVnan’, and ‘nVnan’. Note that what you want may not be possible if it turns out the rows of data aren’t the same length. That means a cell array which is not a problem, but could also mean that your data analysis could be a bit more difficult.

Sign in to comment.

Categories

Find more on Matrices and Arrays 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!