Separating array to individual rows

1 view (last 30 days)
matico
matico on 12 Aug 2015
Hello.
I have an array called "Mat". In every row, there are 3 vectors with same length, one after another. From the last element of the third vector till the end of a row of "Mat", which has 5000 elements, there are "NaN" elements. There are 500 rows in a "Mat" for that combination. After that, the same is repeated two times more. Thus, the end size of a "Mat" is [1500,5000].
You can see how "Mat" is created in a code below. Values of each element here are just for example.
I would like to access to the first vector of each row. It goes from 1:16400, but with different number of elements (1000, 200 or 500). I made this with a for loop. It works, but I am interested, if the same could be achieved without any loop, just using vectorization.
You can better understand what I want, if you look to the code below.
%%Making a matrix Mat
A = [linspace(1,16400,1000) 4000*sin(1:1000) 3800*cos(1:1000)];
B = [linspace(1,16400,200) 4000*sin(1:200) 3800*cos(1:200)];
C = [linspace(1,16400,500) 4000*sin(1:500) 3800*cos(1:500)];
A = [A NaN(1,5000-length(A))];
B = [B NaN(1,5000-length(B))];
C = [C NaN(1,5000-length(C))];
Mat1 = repmat(A,[500 1]);
Mat2 = repmat(B,[500 1]);
Mat3 = repmat(C,[500 1]);
Mat = [Mat1; Mat2; Mat3];
Y = cell(size(Mat,1),1);
for i=1:size(Mat,1)
Y{i} = Mat(i,1:(min(find(isnan(Mat(i,:))))-1)/3);
end
k = Y{1};
p = Y{750};
n = Y{1200};

Answers (0)

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!