compare every element in the first array with the second array
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Akzeptierte Antwort
Jan
am 2 Jun. 2022
Bearbeitet: Jan
am 2 Jun. 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
This does not do, what you need and not, what I expect:
Z = discretize(X, [Y, Y(end) + 1], 'IncludedEdge', 'right')
4 Kommentare
Jan
am 3 Jun. 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
Weitere Antworten (1)
VINAYAK LUHA
am 2 Jun. 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)
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays 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!