need 'for loop' to check 1:32 bit

I have 606,774(1 row means event no.1) event number in 8 column(these are 32 bit values), these numbers are in decimal as shown in below. a=(strVals(1:10,1:8))
a =
0 0 0 0 0 3 0 0
0 0 0 0 0 576 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 48
0 0 1536 8192 0 0 0 0
0 0 0 0 0 0 50331648 0
0 0 0 0 0 6291456 0 0
0 0 0 67108864 0 0 256 0
0 0 0 768 0 0 0 0
0 0 0 4194304 0 0 0 0
This is only for 1:10 event,(I have 606,774 event no.). What I want here is, how to convert these number into binary at same time and how can I check 1:32 bit at once. I have use "bitget" function but I only able to get separately, may I need 'for loop' to get at once?1:32 bit means 1 through 32 bit of binary number(convert above decimal numbers into binary), so I need to know how to write a "for loop" to check each bit one at a time. Like >> bitget(a,1) command checks if the first (smallest/lowest) bit is set for all these numbers at the same time.
Please help me, thanks in advance.

4 Comments

Please clarify what you mean by check 1:32 bit at once. Why do you need to consider the bits of the number?
1:32 bit means 1 through 32 bit of binary number(convert above decimal numbers into binary), so I need to know how to write a "for loop" to check each bit one at a time. Like >> bitget(a,1) command checks if the first (smallest/lowest) bit is set for all these numbers at the same time.Thanks
Why do you need to check all 32 bits for all 606,774 events? What are you looking for in particular?
Actually this is for data analysis in nuclear detector. These above 8 columns are the stretcher value from stretcher 0-7.Each event are related to channel so, I'm looking which channel were hit(means 1). For this we need to convert above number to binary first(up to 32 bit)like for first row dec2bin(3) gives binary number - 00000000000000000000000000000011 that means channel 1&2 were hit and so on. it is not possible to count the bit one by one for 606,774 events. So how to write "for loop" to check each bit one at a time, from this we can identify which bit get 1/0. I hope you understand my question, thanks.

Sign in to comment.

 Accepted Answer

Since you are looking for which channels were hit across all stretchers for all events, then you will need two for loops for the looping of the data, and an additional inner for loop to iterate over the 32 channels. Something like
[m,n] = size(a);
for event=1:m
for stretcher=1:n
% do stuff
end
end
If you are only interested in those stretchers from some event that has a hit channel, then you can exclude all the 0's (since no hits on any channel) and the "do stuff" from above becomes
val = a(event,stretcher);
if val~=0
% now loop over the 32 channels
bitMask = 1;
channelsHit = cast(zeros(1,32),'uint8');
atHit = 0;
% iterate over each channel
for k=1:32
if bitand(val,bitMask)
% is one so a hit on channel k!
atHit = atHit+1;
channelsHit(atHit) = k;
end
% bit shift the mask by one
bitMask=bitshift(bitMask,1);
end
% remove empty elements from array and so we have a variable sized
% array of hit channels for the (event, stretcher) pair
channelsHit = channelsHit(1:atHit);
end
Once you have your list of hit channels for the (event, stretcher) pair, you can save those three pieces of information to a cell array for later analysis.

14 Comments

Thanks a lot, but how to check in window command only file name is enough or what I need to type.
From what you described in your question, I assumed that you had already opened the file, read all of the data, and stored it in the matrix a. Is this not the case? If it is, then just create a function called (for example) bitChecker and pass a as the only input. Use the above code as a template for your algorithm (as I'm not sure what actions you take when you find a channel that has been hit).
If more is required - you only have the file name but have yet to read from it, then check fopen and textscan (assuming your input file is mxn).
I'm using following code, for all bit
>>for ii=1:32
sum( bitand(a, 2^(ii-1) ) > 0 );
end
>> for ii=1:32
results(ii,:)=sum( bitand(a, 2^(ii-1) ) > 0 );
end
>> results
from this we can get 1:32 bit value for all events. But can you please help me to write function to identify which channel/stretcher is not broken.like
4894 4444 4605 4714 7645 5963 6156 3334
This is the value of first bit for all events,like for value 4894 not every channel are good(not broken) so I need to figure out which channel is not broken or which channel is broken(not broken means 1, broken means 0).
I think that you can use the above code (with the mask) and just move across all 32 bits and record which ones are zeros
if bitand(val,bitMask)==0
% so we know that bit k is zero
From that you can decide what to do next.
How to run the function in matlab window to check these things. I need to use all above code in command line or just in file and then run it. Please make me clear.
I suggest creating a MATLAB function that reads in your file, and then uses the above code to start iterating over each line, checking to see if there are any broken channels in any of the stretchers. Try it out, and if you have questions or problems with the function that you have written, then by all means attach it to a comment and I will have a look at it.
I got all values of 1:32 bit by using above code (see in my comment)and I found total number of times hit the channel for all event in simple way. After that I just want to identify among this number which channel is broken or not. I sent you attached file of codes where I used your's code. I think it is too lengthy script to check broken channel and It makes me confused so, Can you please help me to write short cut function to identify broken channels and how to run it.
You should try stepping through the code (with the debugger) to see what is going on….because your script isn't working. In the above answer, the main chunk of code from `val = zeros…' onwards is supposed to be what happens in the inner for loop, where there is the comment % do stuff. I suggest that you try it again, and this time step through the code to see that it does what you want.
Chiranjibi
Chiranjibi on 5 Jun 2014
Edited: Chiranjibi on 5 Jun 2014
I think you may not understand my question, please take a look my attached file and avoid second one. By following first code if I removed sum I got big array of a with o's and 1's, this is kind of crazy things to do and impossible to identify which channel is good(1)as well. So I need to write function to check one's and zero's depend upon my first codes of file. So can you please send me some sample of functions to solve my problem.
Geoff Hayes
Geoff Hayes on 5 Jun 2014
Edited: Geoff Hayes on 5 Jun 2014
I don't understand what you mean by please take a look my attached file and avoid second one. I only see the one attached file so am not sure what the second one is to avoid. Unless you mean avoid the second piece of code and look at that "first" piece which creates a 32x8 array of counts of channels being hit on each detector: the first row corresponds to channel one on all eight detectors. Since all values are non-zero, then the channel worked at least once for all detectors. Row i corresponds to channel i on all eight detectors. Etc.
So how do you identify a broken channel? Is a channel considered broken if it is never set to one for all detectors on all events? You need to define this to solve your problem. Once that has been done, you can use the code that has been presented in this solution (or modify it as you see best) to iterate over all channels for all detectors for all events. But you must attempt this...
Yes, broken channels means it never set to one for all events.So please can you help me to write code that interprets the results to report back which channels look like they are broken with the help of above code that I gave you before.
So after you run the code to produce the 32x8 matrix, then you need to just look for any element that is zero - which would correspond to a broken channel on that detector. With the example results that you posted you could do something like
[R,C] =find(results==0);
R and C will be the row and column indices to the values in results that are zero. (So R will be the channels, and C the detectors.)
Chiranjibi
Chiranjibi on 9 Jun 2014
Edited: Chiranjibi on 9 Jun 2014
Thanks, I got 6 row and 6 column array from this code, and now I need to write function for this file to check broken channel. Like
strVals=readLBCF_File('/data/chocula/lbcfdata/data/ne/raw/VData0101_140101001844',0, 0);
................
for ii=1:32
results(ii,:)=sum( bitand(a, 2^(ii-1) ) > 0 );
end
[R,C] =find(results==0);
for ii=1:32
disp(['stretcher:' num2str(C(ii)) ', channel:' num2str(R(ii)) ' appear to be dead.'])
end
Can you please help me to write function in 2nd line(dot line).
Chiranjibi - The code you pasted above will not always work
[R,C] =find(results==0);
for ii=1:32
disp(['stretcher:' num2str(C(ii)) ', channel:' num2str(R(ii)) ' appear to
be dead.'])
end
There is no guarantee that the number of elements in R or C will be 32. That should have been obvious from the results matrix that you posted in a previous comment.
As well, again in your above comment, there is a dotted line that you are asking help to "fill in" without providing any details or making the attempt yourself. Attempting something, on your own, is important and necessary.
Good luck with your coding of this interesting problem!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!