Convert vector of decimals to matrix order by integer steps
4 views (last 30 days)
Show older comments
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
Accepted Answer
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} );
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!