how to find the equivalent value in the same row of the next column?
1 次查看(过去 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
0 个评论
回答(2 个)
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.
0 个评论
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!