compare every element in the first array with the second array

11 次查看(过去 30 天)
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 个评论
Jan
Jan 2022-6-2
编辑:Jan 2022-6-2
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-6-2
编辑:Jan 2022-6-2
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 个评论
Ahmed Mohamed
Ahmed Mohamed 2022-6-2
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 2022-6-3
@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

请先登录,再进行评论。

更多回答(1 个)

VINAYAK LUHA
VINAYAK LUHA 2022-6-2
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

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by