-3

I have written a code:

edge = linspace(botEdge, topEdge, numBins);
[N,ind] = histc(Speed, edges);
new = find(Speed>edges,1,'last'); 
N(new) = N(new)+1;

For example if I give speed value from data sets say 5.5 they, the new will tell you in which bin this value belongs and you can double check via looking the edge value you can see in which bin this value belongs. I tested with this value and I got correct result. You can also check via running this code with giving input

Speed is 3000x1, edge is 50x1, N is 50x1. This working perfectly for single value of speed but If I want to do same thing with my all speed data (3000x1) what should I do?

EBH
  • 10,350
  • 3
  • 34
  • 59
ravi pandit
  • 117
  • 12
  • 4
    your code makes no sense, you have a for loop i, which is never used inside your loop, you have a vector called speed, and a vector edges in different length, how on earth do you expect to do Speed > edges? Please fix your code. – GameOfThrows Aug 15 '16 at 13:25
  • 1
    You cannot compare `Speed>edges` unless they are the same size or one of them is a scalar, and also it makes no sense. What are you trying to compare hare? – EBH Aug 15 '16 at 14:06
  • @EBH thank you for asking in kind way unlike others (being rude). yes it make sense because I want to know incoming speed data belongs to which bin and here edge is basically a difference between speed(2) -Speed(1) data. So with this edge I can able to see in whichbin this speed data belongs. I modified code written code also for more explanation please have a look : – ravi pandit Aug 15 '16 at 15:36
  • Next time provide **all** relevant information when you first post the question, so you won't get a -4 downvote before you open your mouth... – EBH Aug 15 '16 at 16:03
  • @EBH thank you for your such advice, I will definitely keep in mind – ravi pandit Aug 15 '16 at 16:07
  • Doesn't `ind` already give you this information? `ind` is the bin that each data point went into. – beaker Aug 15 '16 at 19:57
  • @beaker ind is bin no. I have use this to create my reference data and located my reference data on each bin.Now after that new point is coming and I have to find this new point belongs to which bin of reference data. And this can be done by finding the relationship between edge and new point.Hence I used find function to do this. If you understand the explanation please upvote the question – ravi pandit Aug 15 '16 at 20:57
  • @beaker I used histc to create bin for my reference data and I want to relate that bin with my incoming data. So this can only be done via calculating edge hence I just use edge of my reference data with my incoming data,which gave me my answer (i.e incoming data going to which bin of reference data) – ravi pandit Aug 15 '16 at 21:37
  • off course both data is different. reference data is for healthy system and incoming data is for unhealthy system. And Bin edges, specified as a vector. edges(1) is the left edge of the first bin, and edges(end) is the right edge of the last bin. I hope this help you if not I would suggest you to please have a look into a function called histcount. there they have explanation with examples – ravi pandit Aug 15 '16 at 21:50
  • @beaker no friend I am sorry if you feel in that way... Since I am using comment section to explain you hence my explanation is limited here. Hence I told you to refer this site. if you feel I am being rude. My apology for this – ravi pandit Aug 15 '16 at 21:56

1 Answers1

0

Here is a compact but not the most efficient way to do this:

Speed = randi(99,3,1) % some arbitrary data
edge = linspace(1, 99, 50);
New = arrayfun(@(a) find(a>edges,1,'last'),Speed)

Example for output:

Speed =
    22
    41
    64
New =
    11
    20
    32

A for loop will be faster:

New = zeros(size(Speed));
for k = 1:length(Speed)
    New(k) = find(Speed(k)>edges,1,'last');
end

And here is another method:

New = sum(bsxfun(@gt,Speed,edge),2);

All of them yield the same output, it's up to you to check which is fastest under your conditions.

EBH
  • 10,350
  • 3
  • 34
  • 59
  • thank you for your support , I really appreciate your help...could you tell me why you said it is not efficient but compact and could you suggest some efficient way to solve such problem...? – ravi pandit Aug 15 '16 at 15:54
  • @Sujal it's just a matter of timing, If you deal with arrays in size 1*3000 it doesn't really matter, [see here](http://stackoverflow.com/questions/38941694/what-is-the-fastest-way-to-count-elements-in-an-array/) for more details. – EBH Aug 15 '16 at 16:00