I have a three column matrix which consists of three columns (x,y,z) as shown below:
I would like to bin the data in column Y but instead of puting the count in a new column i.e bins, I would like to put the the corresponding value in column Z. For example, I would like to bin the value in column Y (-2.5 in blue cell), but instead of putting the count in bins, I would like to put the value in colum Z (12 in red cell) in that bin.
I have written the code below but its putting the count only:
yy = my_matrix(:,2) % taking the second column
% binning
edges = -2.5:0.3:2.5;
N = histcounts(yy,edges);
new_matrix(:,i)= N;
How can I improve it?

3 Kommentare

Guillaume
Guillaume am 6 Jan. 2020
Typically, you'd have more than one Z per bin (otherwise, it's not really binning!), so what should be put in each bin? The mean? The sum? A vector of values?
Allan Miller
Allan Miller am 6 Jan. 2020
Thanks Guillaume,
So I would like to put an array of values which have a value of 12 in the ith bin and zero elsewhere. i.e, for that example, I would like to put a value of 12 in the bin corresponding to -2.2 : -2.5 and put zeros in other bins from -2.1 : 2.5
Guillaume
Guillaume am 6 Jan. 2020
Bearbeitet: Guillaume am 6 Jan. 2020
Sorry, I don't understand. Here is a simple exampl, given:
% Y Z
M = [-2.5 12
-2.4 15
-2.3 -1
-2.2 5
-2.1 10]
and
edges = [-2.5 -2.2 -1.9];
What exact output do you want?
result = ????
Please write it out as a cell array or matrix.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Max Murphy
Max Murphy am 6 Jan. 2020
Bearbeitet: Max Murphy am 6 Jan. 2020

0 Stimmen

Your code is returning counts only because you are only requesting the first output of histcounts
yy = my_matrix(:,2); % taking the second column
zz = my_matrix(:,3); % according to the diagram
% binning
edges = -2.5:0.3:2.5;
[N,~,bin] = histcounts(yy,edges);
% match the values of zz to the bins that elements
% of yy went into.
%% EDIT %%
zz_in_bins = cell(size(N));
u = unique(bin); % To avoid dealing with empty bins
binIndex = reshape(u,1,numel(u));
for idx = binIndex
zz_in_bins{idx} = zz(bin==idx);
end

7 Kommentare

Guillaume
Guillaume am 6 Jan. 2020
Bearbeitet: Guillaume am 6 Jan. 2020
Assuming that it is what the OP is after, a simpler version of this code would be:
bin = discretize(my_matrix(:, 2), edges);
zz_in_bins = nan(1, max(bin));
zz_in_bins(bin) = my_matrix(:, 3); %WARNING! Unusual behaviour on this line. WARNING!
However, as per the comment I've written, be very aware that the highlighted line in this (now accepted) answer is very unusual. In the generic case, where multiple rows fall within the same bin, then this line will only copy the LAST zz value that corresponds to that bin discarding all other zz values.
So really, this code will only work if there's only one element per bin, in which case, using histogram functions for this is the wrong approach.
Max Murphy
Max Murphy am 6 Jan. 2020
That's a good point, Guillaume. I'll edit the answer since that could be problematic if it's not intended to just swap around the values in z according to what is in y (which as you stated, would have to be uniquely assigned to bins).
To avoid overwriting if you have multiple elements going into the same bin, then you would have to use a cell array and do the indexing slightly differently. I'll change that part in the code above.
Guillaume
Guillaume am 6 Jan. 2020
"To avoid overwriting if you have multiple elements going into the same bin, then you would have to use a cell array and do the indexing slightly differently"
That's what I've been trying to understand from the start. What is actually desired?
Max Murphy
Max Murphy am 6 Jan. 2020
It is hard to tell from the example that was given, but I will say in practice I've used a strategy similar to this to associate objects with a particular "bin." In my case, I was interested in showing the graphic using an image made from the bin counts, but then I still wanted to be able to relate user interactions with the image (e.g. where the user defined a convex hull polygon) with the original objects that contributed to the increased pixel intensity in the bounded region. I did it that way because otherwise there were far too many graphics objects to plot the time-series snippets I wanted to display.
Allan Miller
Allan Miller am 13 Jan. 2020
Okay, Thanks a lot for your responses - In case I wasnt clear in my question, let me try to rephrase it:
I have some datapoints in column X and column Y of a matrix. I want to put a data point of column X into a bin specified by: -2.5:0.3:2.5. but the process of binning put the count in that bin right? I do not want to put the count, but I would lilke to put the value of that corresponding data point in column Y instead. So in that example given above, instead of putting 1 in a bin between say -2.5 : -2.2, I would like to put 12 in that bin.
Guillaume
Guillaume am 13 Jan. 2020
I think we understood that. The problems come when you have several X that falls into the same bin. Which of the matching Y values goes into the bin? You never answered my question so we don't know.
If there is only ever one X and Y per bin, then you're not actually doing any binning and there are much simplers ways to achieve what you want.
Max Murphy
Max Murphy am 13 Jan. 2020
Indeed, if there is one X and Y per bin, I would not follow the code that I've posted above, and instead use discretize as suggested by Guillaume and Steven elsewhere in this thread.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 6 Jan. 2020

0 Stimmen

Use the discretize function. See the "Assign Bin Values" example on that documentation page as I believe it does what you're trying to do.

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by