how to find the equivalent value in the same row of the next column?
3 ビュー (過去 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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!