Compare all elements of a vector against each other

50 次查看(过去 30 天)
I have a vector that includes some values
v=[2.4 3.5 7.4 3.6 4.5]
each value is the mean value of a row in a matrix called y
now I want to compare each value with the other values in the vector and if they are similar (with a deviation of 10%) then delete the row in y of the larger value.
I managed to compare just the neighborhood values
for i=1:length(v)-1
if v(i)==v(i+1)
y(i,:)=[];
end
end
I am struggling to find the right way to compare the values against each other and to include this 10%
Any help is much appreciated !
  5 个评论
monmatlab
monmatlab 2016-12-12
so basically you compare 2.4 to 3.5 2.4 to 7.4 2.4 to 3.6 2.4 to 4.5
then you move on to the next value 3.5 to 7.4 3.5 to 3.6 3.5 to 4.5
and so on
jahan jahan
jahan jahan 2022-9-12
Dear.
I need checking numbers inside the vector if they inside specific limit. If yes the results equal to 1 otherwise 0. The results will be new vector
Thank you

请先登录,再进行评论。

采纳的回答

Adam
Adam 2016-12-12
编辑:Adam 2016-12-12
relativeDiffs = abs( ( v - v' ) ) ./ v; % R2016b syntax only
indicatorMatrix = relativeDiffs <= 0.1;
will give you an indicator matrix of values to delete. Obviously you would ignore the leading diagonal as this is each value against itself. The values are normalised against the original v by row so the upper right triangle and lower left triangle will differ by the fact that e.g.
(3.5 - 2.4) / 2.4 ~= (3.5 - 2.4) / 3.5
You will get both of these answers from the relativeDiffs matrix. In some cases only one of these will highlight in the final indicator matrix as being < 10%.
relativeDiffs = abs( bsxfun( @minus, v, v' ) ) ./ v
should work for pre R2016b syntax.

更多回答(1 个)

Guillaume
Guillaume 2016-12-12
If v is the mean of the rows (e.g. obtained with v = mean(y, 2)), shouldn't it be a column vector rather than a row vector as in your example.
In any case, if I understood correctly, this should work:
v = mean(y, 2);
inrange = v >= 0.9*v.' & v <= 1.1*v.'; %In r2016b only
inrange = inrange .* ~eye(numel(v)); %diagonal will always be 1 as a number is in range of itself. set to 0 instead.
y(any(inrange, 2), :) = []; %delete rows of y whose mean is in range of any other row mean
In versions prior to R2016b, replace the relevant line by:
inrange = bsxfun(@ge, v, 0.9*v.') & bsxfun(@le, v, 1.1*v.');

类别

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