Comparing arrays and calculating

1 回表示 (過去 30 日間)
Adeela Khatoon
Adeela Khatoon 2019 年 5 月 28 日
回答済み: Shubham Gupta 2019 年 5 月 28 日
I have two column arrays: A1 and A2. For A1(1,1) I need to find two values in A2 that bracket A(1,1). In other words, a value just greater than A1(1,1) and another just less than A1(1,1). Using these greater than and less than values I need to create a new array, A3, which is the average of the two values.
Then repeat this process for the rest of the rows in A1. Following is my attempt at doing this, but it has proven to be quite unsuccessful. I need urgent help.
My idea was to find the index of the value in A2 that is just greater than A(1,1). Using that index I can calculate the average.
% Read Array 1: Data obtained from EDDYBL
A1 = readmatrix('Array1.xlsx');
% Read Array 2: Data obtained from GridPro
A2 = readmatrix('Array2.xlsx');
% Assuming the first column contains y-values
for i = 1:length(A2)
for j = 1:length(A1)
w = find(A2(i) < A1(j)) % The index for which the value is just greater than A(1,1)
A3(i,1) = (A1(w,1) + A1(w-1,1))/2
disp(w)
end
end

採用された回答

Shubham Gupta
Shubham Gupta 2019 年 5 月 28 日
Try this :
for i = 1:length(A2)
[Alh,im] = sort(A2);
w1 = im(find(Alh>A1,1)); % The index for which the value is just greater than A(1,1)
w2 = im(find(Alh<A1,1,'last'));
A3(i,1) = (A1(w1,1) + A1(w2,1))/2;
end
For more info on "find" use help in Command Window. I hope it helps !

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by