compare every element in the first array with the second array

11 vues (au cours des 30 derniers jours)
Ahmed Mohamed
Ahmed Mohamed le 2 Juin 2022
Commenté : Jan le 3 Juin 2022
I have two arrays
X = [1,3,5,9]
Y = [1,2,4,9]
I want a function to compare every element in array X with the whole array Y and if this element exists in array Y then write the element and if this element doesn't exist in array Y then add 1 until it find the closest number.
If we suppose the output is Z so Z should be like that Z = [1,4,9,9]
Thanks in advance
  1 commentaire
Jan
Jan le 2 Juin 2022
Modifié(e) : Jan le 2 Juin 2022
Are the arrays sorted?
What is the purpose of "add 1 until it find the closest number"? The closest larger number is the closest larger one without adding 1 also.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 2 Juin 2022
Modifié(e) : Jan le 2 Juin 2022
X = [1,3,5,9];
Y = [1,2,4,9];
Z = zeros(size(X));
for k = 1:numel(X)
Z(k) = Y(find(Y >= X(k), 1));
end
Z
Z = 1×4
1 4 9 9
This does not do, what you need and not, what I expect:
Z = discretize(X, [Y, Y(end) + 1], 'IncludedEdge', 'right')
Z = 1×4
1 2 3 3
  4 commentaires
Ahmed Mohamed
Ahmed Mohamed le 2 Juin 2022
I took your code and replace X,Y with N_OMNI, Nvalues
Then I run the code and Z didn't contain the whole numbers and I can't figure out what I should do to solve this issue.
Can You take my hand?
Jan
Jan le 3 Juin 2022
@Ahmed Mohamed: Why do you declare the loop counters i and j as symbolic variables?
sidb = 10:25;
si = 10.^(sidb./10);
n = 3:0.1:4;
k = 504; % channel
% No, loop counters should not be symbolic:
% syms i j integer
% Here you pre-allocate a 15x15 matrix, but Nvalues is
% overwritten repeatedly later:
Nvalues = zeros(15); %n values
% Better pre-allocate N:
N = zeros(16, 16);
% Not used hre: temp= [0];
for i = 0:15
for j = 0:15
N(i+1, j+1) = i^2 + i*j + j^2;
% Nvalues=N; % Overwritten in each iteration
% Simply move this line behind the loops
end
end
% A cheaper version of the loops above:
N = (0:15) .* (0:15).';
PB = [0.01, .01, 1];
N_OMNI = zeros(11,16);
N_120 = zeros(11,16);
N_60 = zeros(11,16);
for i=1:16
for x=1:11
N_OMNI(i,x) = (6*si(i))^(2/n(x)) / 3;
N_OMNI(i,x) = ceil(N_OMNI(x));
% Where is 2nd index ^ do you mean N_OMNI(i,x)
N_120(i,x) = (6*si(i))^(2/n(x)) / 3;
N_120(i,x) = ceil(N_120(x));
% Where is 2nd index ^ do you mean N_120(i,x)
N_60(i,x) = (6*si(i))^(2/n(x)) / 3;
N_60(i,x) = ceil(N_60(i,x));
% N_OMNI, N_120 and N_60 have the same values?!?
end
end
Z = zeros(size(N_OMNI));
for w = 1:numel(N_OMNI)
Z(w) = N(find(N >= N_OMNI(w), 1));
end

Connectez-vous pour commenter.

Plus de réponses (1)

VINAYAK LUHA
VINAYAK LUHA le 2 Juin 2022
Hi,
Your question can also be framed as -
"For every element x in X , find the least element in y in Y , such that y>=x"
This problem can also be solved using binary search in O(|X||Y|log(|Y|) time, (here |X| represents cardinality of X) ,provided we sort the Y first .
Here's the code for my approch to the problem .
ASSUMPTION
Since you did not clarify on the output when any x in X is larger than the maximum value in Y , I assume it as -1 .
X=[5 4 -10 -2 5 8 10 88 74 5];
Y=[2 3 4 9];
your_ans =fun(X,Y)
your_ans = 1×10
9 4 2 2 9 9 -1 -1 -1 9
function val = helper(x,Y)
n=length(Y);
l=1;r=n;
while(l<=r)
mid =floor(l+(r-l)/2);
if(x==Y(mid))
val=Y(mid);
return;
elseif(x<Y(mid))
r=mid-1;
else
l=mid+1;
end
end
if(l>length(Y))
val=-1;
else
val=Y(l);
end
end
function ret =fun(X,Y)
ret=zeros(1,length(X));
sort(Y);
for i=1:length(X)
ret(i)=helper(X(i),Y);
end
return
end

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by