How can i plot a contour plot with the information below?

I have a 57 x 3 vector where the first 2 columns represent the x and y coordinates respectively and I'd like to join the coordinates with a curve based on the value in the 3rd column.
i.e. all coordinates with a "1" in the third column will be joined by a single curve, all coordinates with a "2" in the third column will be joined by a separate curve and so on, until all 57 points have been plotted.
The vector is called "new" shown below:
0.814723686393179 0.959291425205444 1
0.905791937075619 0.547215529963803 1
0.126986816293506 0.138624442828679 1
0.913375856139019 0.149294005559057 1
0.632359246225410 0.257508254123736 1
0.0975404049994095 0.840717255983663 1
0.278498218867048 0.254282178971531 1
0.546881519204984 0.814284826068816 1
0.957506835434298 0.243524968724989 1
0.964888535199277 0.929263623187228 1
0.157613081677548 0.349983765984809 1
0.970592781760616 0.196595250431208 1
0.957166948242946 0.251083857976031 1
0.485375648722841 0.616044676146639 1
0.800280468888800 0.473288848902729 1
0.141886338627215 0.351659507062997 1
0.421761282626275 0.830828627896291 1
0.915735525189067 0.585264091152724 1
0.792207329559554 0.549723608291140 1
0.959492426392903 0.917193663829810 1
0.655740699156587 0.285839018820374 1
0.0357116785741896 0.757200229110721 1
0.849129305868777 0.753729094278495 1
0.933993247757551 0.380445846975357 1
0.678735154857774 0.567821640725221 1
0.757740130578333 0.0758542895630636 1
0.743132468124916 0.0539501186666072 1
0.392227019534168 0.530797553008973 1
0.655477890177557 0.779167230102011 2
0.171186687811562 0.934010684229183 2
0.706046088019609 0.129906208473730 2
0.0318328463774207 0.568823660872193 2
0.276922984960890 0.469390641058206 2
0.0461713906311539 0.0119020695012414 2
0.0971317812358475 0.337122644398882 2
0.823457828327293 0.162182308193243 2
0.694828622975817 0.794284540683907 2
0.317099480060861 0.311215042044805 2
0.950222048838355 0.528533135506213 2
0.0344460805029088 0.165648729499781 3
0.438744359656398 0.601981941401637 3
0.381558457093008 0.262971284540144 3
0.765516788149002 0.654079098476782 3
0.795199901137063 0.689214503140008 3
0.186872604554379 0.748151592823710 3
0.489764395788231 0.450541598502498 3
0.445586200710900 0.0838213779969326 3
0.646313010111265 0.228976968716819 4
0.709364830858073 0.913337361501670 4
0.754686681982361 0.152378018969223 4
0.276025076998578 0.825816977489547 4
0.679702676853675 0.538342435260057 5
0.655098003973841 0.996134716626886 5
0.162611735194631 0.0781755287531837 5
0.118997681558377 0.442678269775446 5
0.498364051982143 0.106652770180584 5
0.959743958516081 0.961898080855054 5

Answers (2)

new = [ ...];
[unique_labs, lab_idx] = unique(new(:,3)); %I do not rely upon them being integer or consecutive
num_unique = length(unique_lab);
for K = length(unique_lab) : -1 : 1
this_label = unique_labs(K);
this_xy = new(lab_idx == K, 1:2);
line_handles(K) = plot(this_xy(:,1), this_xy(:,2), 'DisplayName', sprintf('group %g', this_label) );
end
legend( line_handles, 'show')

2 Comments

Hi Walter,

What is the purpose of:

this_xy = new(lab_idx == K, 1:2);

I tried to run the code and got a Subscripted assignment dimension mismatch error on the following line.

If we knew for sure that the labels were consecutive integers starting from 1, then the code could be shortened to
for K = 1 : 5
this_xy = new(new(:,3) == K, 1:2);
line_handles(K) = plot(this_xy(:,1), this_xy(:,2), 'DisplayName', sprintf('group %g', K) );
hold on
end
hold off
legend(line_handles, 'show')
and if we knew that there were no other items plotted we could shorten further to
for K = 1 : 5
this_xy = new(new(:,3) == K, 1:2);
plot(this_xy(:,1), this_xy(:,2));
hold on
end
hold off
legend('group 1', 'group 2', 'group 3', 'group 4', 'group 5');

Sign in to comment.

[~,ia,~]=unique(new(:,3));
ia=[ia;size(new,1)];
for i=1:numel(ia)-1
figure(i);
contour(new(ia(i):ia(i+1),1:2))
end

Categories

Asked:

on 28 Mar 2018

Commented:

on 28 Mar 2018

Community Treasure Hunt

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

Start Hunting!