How to make a P color plot with two variables

Hi I wish to make a P colour plot with the following data (see attached csv). However, I only have 2 variables (Elv and KE) and I want to add a 3rd variable (Count rate) as from the data we notice various instances of the same values occurring more than once. Any help would be much appreciated. The sample data looks like this:
Elv KE
88 8600
88 8600
88 8600
88 8600
88 8600
88 8600
88 8600
88 8600
88 8600
88 8600

 Respuesta aceptada

Sam Cook
Sam Cook el 13 de Jun. de 2018
You could use unique to get each unique combination of your row data, and then iteratively count the number of occurrences.
data = [[88; 88; 88; 23; 23], [8600; 8600; 9000; 9000; 9000]];
opts = unique(data, 'rows'); % Unique combinations of columns
nopts = size(opts, 1);
counts = zeros(nopts, 1);
for i=1:nopts % Counts the number of exact matches
counts(i) = nnz(all(data == opts(i,:), 2));
end
dataout = [opts counts];
This takes the example data...
88 8600
88 8600
88 9000
23 9000
23 9000
...and produces this:
23 9000 2
88 8600 2
88 9000 1
which I believe is the information that you're looking for.

3 comentarios

Thank you for your response. If I had a csv which looks like the following:
"Ion N","Mass","Charge","X","Y","Z","Azm","Elv","KE"
3849094,0.00054858,-1,66.5216,-51,-3.8,-180,88.7,18160
3849094,0.00054858,-1,27.3925,30.3532,-4.07076,-177.1,41.5494,17697.2
3849095,0.00054858,-1,66.5216,-51,-3.7,-180,88.7,18160
3849095,0.00054858,-1,26.6277,31.0039,-3.91402,-177.096,40.8293,17699.4
3849096,0.00054858,-1,66.5216,-51,-3.6,-180,88.7,18160
3849096,0.00054858,-1,4.125,44.9887,-2.47517,-176.363,25.715,17711.1
How can I group 'KE' and 'Elv' to make a count for both these columns. I attempted the following code but it hasn't worked so far.
rea = detetctImportOptions('Monday.csv');
data = readtable('Monday.csv', rea);
opts = unique(data, 'KE, 'Elv');
nopts = size(opts, 1);
counts = zeros(nopts, 1);
for i = 1:opts
counts(i) = nnz(all(data == opts(i,:), 2));
end
dataout = [opts counts];
Apologies for the late response. First you need to get the subset of data (EG the 'Elv' and 'KE' columns), and then perform the same algorithm as above.
dataSubset = data{:, {'Elv','KE'}};
opts = unique(dataSubset, 'rows');
nopts = size(opts, 1);
counts = zeros(nopts, 1);
for i = 1:nopts
counts(i) = nnz(all(subsetData == opts(i,:), 2));
end
dataout = [opts counts];
Hope this helps.
ariane
ariane el 26 de Jun. de 2018
Thank you very much for responding to my question. This works perfectly.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Objects en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Jun. de 2018

Comentada:

el 26 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by