Based on Column 3 values how to get specific column 1 values?

I have 3 columns of data coming from excel, Please find attached txt file, for instance in the attached txt file when column C is '0', i would like to get the value of column A at that point and the two more values above it i.e 2301,2300,2244 in the attached file.

 採用された回答

jonas
jonas 2018 年 10 月 23 日
編集済み: jonas 2018 年 10 月 24 日

1 投票

fid = fopen('sample.txt')
out = textscan(fid,'%f%f%f','headerlines',1);
fclose(fid)
A = cell2mat(out);
id0 = find(A(:,3) == 0);
idc = [id0,id0-1,id0-2];
idc = unique(idc)
idc(idc<1) = [];
A(idc, 1)
ans =
2244 2300 2301
works for multiple zeros as well

6 件のコメント

raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 24 日
Hey Jonas, how to get the corresponding column C values
jonas
jonas 2018 年 10 月 24 日
編集済み: jonas 2018 年 10 月 24 日
What do you mean? For other values such as 0.5 and 1? Just change this line
id0 = find(A(:,3) == 0);
change here
Note that I've updated the answer. There was a bug previously where the script would return an error if the target was either the first or the second row, because in that situation "two previous values" does not exist.
raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 24 日
no, i mean 2244,2300, 2301 are column A values, so i want the corresponding column c values for them, which would be (1,0.5,0) i guess and thanks for bug fixing
jonas
jonas 2018 年 10 月 24 日
編集済み: jonas 2018 年 10 月 24 日
A(idc, 3)
Should do it. Have not tested it myself.
raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 24 日
Yup, worked. Thank you Jonas
jonas
jonas 2018 年 10 月 24 日
Cheers!

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2018 年 10 月 23 日

コメント済み:

2018 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by