Moving Y coordinate to a new column for corresponding X values
Show older comments
I am new to using Matlab and have got hung up on a small problem and really appreciate any help. Using segmentation; converging the image to binary image and indexing I have created the array below, where column one is X and column two is Y.
2 155; 2 345; 3 155; 3 345; 4 154; 4 345
My aim is to move Y coordinate a new column for corresponding X values, so that I have columns X, Y1, Y2,...Yn, so the matrix becomes
2 155 345; 3 155 345; 4 154 345;
thank you in advance for taking a look.
Answers (2)
Star Strider
on 23 Jan 2015
Use reshape (and unique):
M = [2 155; 2 345; 3 155; 3 345; 4 154; 4 345];
[N,ia,ic] = unique(M(:,1));
Mr = [M(ia) reshape(M(:,2), 2, [])'];
2 Comments
nick8631
on 25 Jan 2015
Thanks for the reply, however I have some errors when then this code is applied to the whole matrix
(Error using horzcat Dimensions of matrices being concatenated are not consistent. Error in lt4 (line 22) Mr = [M(ia) reshape(M(:,2), 2, [])']; )
The problem I seem to be having is some time there are more than 2 values corresponding to the x-axis, is it possible to create nth columns for the nth corresponding values. For example it fails for the data below...
3 155; 3 345; 4 154; 4 345; 5 153; 5 154; 5 345; 6 151; 6 152; 6 153; 6 345; 7 150;
Thanks nick
Star Strider
on 25 Jan 2015
I couldn’t test my code on the whole matrix!
This works:
M = [3 155; 3 345; 4 154; 4 345; 5 153; 5 154; 5 345; 6 151; 6 152; 6 153; 6 345; 7 150];
[N,ia,ic] = unique(M(:,1));
for k1 = 1:size(N,1)
Mr{k1} = [N(k1) M(ic == (k1),2)'];
end
celldisp(Mr) % Display Result
dpb
on 23 Jan 2015
If it's always two pair as shown, then simply selection and rearranging works quickly...
>> xy=[2 155; 2 345; 3 155; 3 345; 4 154; 4 345];
>> [xy(1:2:end,1) reshape(xy(:,2),2,[]).']
ans =
2 155 345
3 155 345
4 154 345
>>
1 Comment
nick8631
on 25 Jan 2015
Thank you for your reply however there are often variation from 2-7 rows of corresponding values.
Thanks nick
Categories
Find more on Image Arithmetic 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!