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

2 次查看(过去 30 天)
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

回答(2 个)

Prasanth Sikakollu
Prasanth Sikakollu 2019-6-10
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 2019-6-10
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]

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by