Compare values in array1 with array2 and store a new value in a new array if match found

8 次查看(过去 30 天)
I have a problem of comparing two arrays on Matlab scripting and below I have explained.
%Array1 has one column
Array1=[1.56357468109619
1.61046126078761
1.81562025010618
1.68554263182706
2.01705143067582
2.20087755213663
1.90411420706440
1.86094752726691
1.92177045043986
1.78486562784988
1.85660250759791
1.53693897473804
2.01744530015071
1.90897780937155]
%Array2 has two columns
Array2=
2.02888000000000 600;
1.80918000000000 500;
1.65728000000000 400;
1.52849000000000 300;
1.35517000000000 200;
1.13304000000000 100;
1.00398000000000 0
I want to compare each value of the single column in Array1 with the values in column 1 of Array2. If the value of Array1 matches or fits within a range or within a given limit range with a value in column1 of Array2, I wish to create a new array (Array3) with the corresponding value in Column2 of Array2.
Example: 1st value in column1 of Array1 = 1.61046126078761 Since this value is closest to 3rd value of column1 in Array2, I want to assign the first value in Array3 as 400. Likewise I want to check all the values in Array1 and assign the values and create the new Array3
Would be great if someone could help me out! Thanks in advance!
Best wishes Sarala

采纳的回答

Stephen23
Stephen23 2017-6-29
编辑:Stephen23 2017-6-29
>> D = abs(bsxfun(@minus,Array1,Array2(:,1).'));
>> [~,X] = min(D,[],2);
>> Array3 = Array2(X,2)
Array3 =
300
400
500
400
600
600
500
500
  2 个评论
SNT
SNT 2017-6-30
Hi Stephen
Thank you very much for your reply. This works well! But could you help me understand what happens within this code section.
Thanks!
Sarala
Stephen23
Stephen23 2017-6-30
编辑:Stephen23 2017-6-30
D is the absolute values of the differences between all values in both input vectors. It is a matrix corresponding to the values of Array1 down the rows, and of Array2 across the columns.
X is the column index of the minimum value in each row of that matrix.
Then index X gets the required rows from Array2.
For more info on the specific operations read their documentation, and do the introductory tutorials:

请先登录,再进行评论。

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2017-6-29
编辑:Andrei Bobrov 2017-6-30
A = sortrows(Array2,1);
out = A(discretize(Array1,[-inf;A(1:end-1,1)+diff(A(:,1))/2;inf]),2);
  2 个评论
SNT
SNT 2017-6-30
Hi Andrei,
I had a few errors popping up
Cell contents assignment to a non-cell array object
So I added
A={};
But then I get below error
Error using discretize Expected input number 2, edges, to be non-decreasing valued.
Error in discretize (line 54) validateattributes(edges, {'numeric','logical'},{'vector', 'real', ...
Error in CompareArrays (line 41) A{3} = A{2}(discretize(A{1},[-inf;A{2}(1:end-1,1)+diff(A{2}(:,1))/2;inf]),2);
Thanks!
Sarala

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by