Extract cell value based on row contents

2 次查看(过去 30 天)
I have a table with 4 columns (id1, id2, HB, and FENE) and many thousands of rows. From this I would like to create a new table of HB data, extracted if the id1 and id2 contain particular values. For instance:
id1 id2 HB FENE
1 28 0.718 281
1 31 0.572 256
1 07 0.182 413
2 28 0.271 361
2 31 0.725 249
2 07 0.012 992
1 28 0.381 438
2 31 0.991 913
if I am interested in the id combinations of [1,28] and [2,31], I want to produce the table:
1+28 2+31
0.718 0.725
0.381 0.991
In the dataset there will always be the same number of id combinations (i.e. if there are five rows containing [1,28], there are also five rows of [2,31]).
My current process is:
id1 = [1 2 3 4 5]
id2 = [28 31 34 37 40]
dataFile = file.txt
dataOutput = zeros(1,5)
for k = 1:5
head1 = strand1(k)+"+"+strand2(k)
if dataFile{:,1} == strand1(k)
if dataFile{:,2} == strand2(k) %both conditions must be true to proceed
?? extract data from this row, in col 3, and append to dataOutput in col k, new row
end
end
%loops in k=1 until all instances are found
%do i have to define how many times to loop for k=1?
end
outputTable = array2table(dataOutput)
outputTable.Properties.VariableNames(1:5) = {'head1','head2','head3','head4','head5'}
Thank you very much for any pointers or advice.

采纳的回答

Askic V
Askic V 2023-2-13
编辑:Askic V 2023-2-13
Here is one example that should give you an idea how to proceed:
A = [1 28 0.718 281
1 31 0.572 256
1 07 0.182 413
2 28 0.271 361
2 31 0.725 249
2 07 0.012 992
1 28 0.381 438
2 31 0.991 913];
ind = find((A(:,1) == 1) & (A(:,2) == 28))
ind = 2×1
1 7
A_1_28 = A(ind,3)
A_1_28 = 2×1
0.7180 0.3810

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by