How to find values for corresponding start and end positons

6 次查看(过去 30 天)
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 个评论
Akshay Malav
Akshay Malav 2019-6-20
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 2019-6-21
Capture.JPG
So i want the position of bit 1(colored) and its corresponding values of bit2 to be displayed

请先登录,再进行评论。

回答(2 个)

madhan ravi
madhan ravi 2019-6-21
编辑:madhan ravi 2019-6-21
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 2019-6-21
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!

Translated by