How to find values for corresponding start and end positons

12 views (last 30 days)
My data is as follows stored in excel.
bit bit2
1 3
1 5
1 22
1 25
0 10
0 15
1 17
1 19
1 12
I had to find the position of first and last 1 bit of each set of 1 .. so i got answer lik this
start 1 7
end 4 9
Now i have to print or save (excel or word) bit2 values for corresponding start and end positons
  2 Comments
Akshay Malav
Akshay Malav on 20 Jun 2019
Can you please elaborate your statement regarding finding position of first and last 1 bit and also the start and end mentioned by you
Wind flower
Wind flower on 21 Jun 2019
Capture.JPG
So i want the position of bit 1(colored) and its corresponding values of bit2 to be displayed

Sign in to comment.

Answers (2)

madhan ravi
madhan ravi on 21 Jun 2019
Edited: madhan ravi on 21 Jun 2019
Getting the start and positions , i believe you know it from the previous question, so don‘t waste time using a loop.
bit1 =[...
1
1
1
1
0
0
1
1
1]
bit2 =[...
3
5
22
25
10
15
17
19
12];
Start = [1,7];
End = [4,9];
Positions = [Start;End].'; % first column represents start of each group and the second the end.
Sets = arrayfun(@(x,y)bit2(x:y),Positions(:,1),...
Positions(:,2),'un',0)

Akshay Malav
Akshay Malav on 21 Jun 2019
Here is the sample code
filename = 'mysheet.xlsx'; %read the excel filr
A = xlsread(filename); %store the content in A Matrix
[row col] = size(A); %find the size of the Matrix
array = []; %array which will have the bit2 value
i=1;
while i<=row %iterating through the bit 1 column
j=i;
if A(j,1)==1 % the starting point of the set of 1
array = [array A(j,2)]; % store the corresponding bit2 in array
while j<=row & A(j,1)==1 % loop till we find 0
j=j+1;
end
array = [array A(j-1,2)]; %store the final bit2 value
i=j;
else % update i
j=j+1;
i=j;
end
end

Community Treasure Hunt

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

Start Hunting!