Convert vector of decimals to matrix order by integer steps

4 views (last 30 days)
Hello all, my first post and first loop.
Want I would like to achieve is to rearrange a record of data composed out of decimal numbers, e.g. V=[1.1 1.3 1.5 1.6 1.9 2 2.1 2.5..3.2 3.6..4] to a matrix in which its columns are represented by each integer step. M=[1.1 1.3 1.5 1.6 1.9;2 2.1 2.5...;3 3.2 3.6...;4]'
Now I thought of doing this with a loop the following way
V=Drafts96(:,1);
j=1
for i=1:length(V);
if V(i,1)<ceil(V(i,1));
M(i,j)=t4(i);
else V(i,1)>=ceil(V(i));
j=j+1;
M(i,j)=V(i);
end
end
My thoughts behind this are, go through each value and check if it is less than the ceil of the number, if so assign it row i from the column j. If not, assign a new column (j+1) in the matrix to it.
Needless to say that this isn't working out. Could you please help me! If you can add the improved code even better. Thanks
  3 Comments
ZyMag
ZyMag on 8 Nov 2012
Hi Matt, thanks for the reply. The number of elements (decimal numbers) in between the integers is NOT the same. This means that I wont be able to make a matrix out of it. Is my loop then pointless? Im reading about structures at the moment..any suggestions for that?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Nov 2012
This is not possible in MATLAB. Numeric matrices in MATLAB must have the same number of entries in every row.
You can use filler values such as NaN for empty positions, or you can use a cell array.
M = accumarray( floor(V), V, [], @(R) {R} );
  2 Comments
ZyMag
ZyMag on 9 Nov 2012
The vector rearrangement works good, but what if V is a nx2 matrix instead of a vector and I would like to add the values of this second column to the same sub division I have just made. So in each cell I would obtain a matrix that are arranged according to the first column of it, i.e. the decimal numbers with its accompaning value of the second column. I know VAL only allows for vectors, so how can I manage?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!