how to find the equivalent value in the same row of the next column?

3 views (last 30 days)
I want to know the value in the same row in the next column of the last repeating value in column 1, for example in column 1 the value 28 stops in row 3, then its corresponding value in column 2 is 13.
28 1
28 2
28 13
30 11
30 22
30 30

Answers (2)

Prasanth Sikakollu
Prasanth Sikakollu on 10 Jun 2019
Use a for loop to iterate over the values in 1st column and if the required condition is satisfied, append the value in the 2nd column of respective row to the answer.
The following code does that.
data = [28 1; 28 2; 28,13; 30 11; 30 22; 30 30];
cur_val = data(1,1);
ans = [];
for i = 1:length(data)
if cur_val ~= data(i,1) % checking if it is the last repeating value in column 1
ans = [ans data(i-1,2)]; % appending the value in the 2nd column of the previous row to the ans
cur_val = data(i,1);
end
end
ans = [ans data(end,1)];
Hope it helps in solving your problem.

Raj
Raj on 10 Jun 2019
Use this:
data = [28 1; 28 2; 28,13; 30 11; 30 22; 30 30];
A=diff(data(:,1));
data(:,1)=[A;0];
temp=find(data(:,1)~=0);
temp1=data(end);
Required_Answer=[data(temp,2) ;temp1]

Community Treasure Hunt

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

Start Hunting!