Extract the index and find the value in other array that have the same extracted index?
1 view (last 30 days)
Show older comments
I have many arrays but in my question I'll minimize it to be understood for you.
if I have
t1=[ 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000]
the corresponsing x for t1 is
x1=[ 0.1690 0.6491 0.7317 0.6477 0.4509 0.5470]
I have other iteration with different length than t1,x1 as showing below:
t2=[
1.0000 1.0100 1.0200 1.0300 1.0400 1.0500 1.0600
1.0700 1.0800 1.0900 1.1000 1.1100 1.1200 1.1300
1.1400 1.1500 1.1600 1.1700 1.1800 1.1900 1.2000
1.2100 1.2200 1.2300 1.2400 1.2500 1.2600 1.2700
1.2800 1.2900 1.3000 1.3100 1.3200 1.3300 1.3400
1.3500 1.3600 1.3700 1.3800 1.3900 1.4000 1.4100
1.4200 1.4300 1.4400 1.4500 1.4600 1.4700 1.4800
1.4900 1.5000
]
the corresonsing x2 for t2 is
x2=[
0.4427 0.1067 0.9619 0.0046 0.7749 0.8173 0.8687 0.0844 0.3998 0.2599
0.8001 0.4314 0.9106 0.1818 0.2638 0.1455 0.1361 0.8693 0.5797 0.5499
0.1450 0.8530 0.6221 0.3510 0.5132 0.4018 0.0760 0.2399 0.1233 0.1839
0.2400 0.4173 0.0497 0.9027 0.9448 0.4909 0.4893 0.3377 0.9001 0.3692
0.1112 0.7803 0.3897 0.2417 0.4039 0.0965 0.1320 0.9421 0.9561 0.5752
0.0598
]
I need for loop that can extarct the index for t2 that has same value in t1, then after finding the index for same value in t2 with t1 , find the coressponding x2 for each t2 that have same value in t1.
the output will be new x which is extracted from x2 and have same length of x1
2 Comments
Sargondjani
on 5 Feb 2019
Edited: Sargondjani
on 5 Feb 2019
Have a look at 'find'. there are some examples that will help you further. You can for example get row and column index and use a for loop over t1, with
[row,col]= find(t2 == t1(it1))
, something like that.
Accepted Answer
Bob Thompson
on 5 Feb 2019
You shouldn't actually need a for loop to do this, just some logic indexing.
goal = x2(ismember(t2,t1));
1 Comment
Stephen23
on 6 Feb 2019
Edited: Stephen23
on 6 Feb 2019
Keep in mind that floating point error means that checking for exact equivalence of decimal values is not recommended, and can easily lead to unexpected outputs. For the data shown in the question it would be much more robust to use ismembertol.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!