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

11 Ansichten (letzte 30 Tage)
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
  2 Kommentare
Stephen23
Stephen23 am 29 Jun. 2017
"Example: 1st value in column1 of Array1 = 1.61046126078761"
Actually that is the second element in Array1, not the first.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Jun. 2017
Bearbeitet: Stephen23 am 29 Jun. 2017
>> 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 Kommentare
SNT
SNT am 30 Jun. 2017
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 am 30 Jun. 2017
Bearbeitet: Stephen23 am 30 Jun. 2017
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:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 29 Jun. 2017
Bearbeitet: Andrei Bobrov am 30 Jun. 2017
A = sortrows(Array2,1);
out = A(discretize(Array1,[-inf;A(1:end-1,1)+diff(A(:,1))/2;inf]),2);
  2 Kommentare
SNT
SNT am 30 Jun. 2017
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by