Does matlab have any function that can compare multiple numbers and return logical value zero or one?
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zeynab Mousavikhamene
am 31 Jan. 2020
Bearbeitet: GeeTwo
am 27 Dez. 2022
I need to find a matlab function that does this:
F(2,3,4)=0
F(2,2,2)=1
F(5,6,7,8,9,2)=0
F(6,6,6,6,6,6,6,6)=1
if all inputs that are numeric are equal, the function returns 1 and if all are not equal, function returns 0. In the first exaple, 2 and 3 and 4 are not equal so the function returned 0. In the second example all inputs are 2 so the function returned 1. Does matlab have such function?
2 Kommentare
David Hill
am 31 Jan. 2020
You need to explain more. I cannot determine what you want. F cannot be 3,6, and 8 dimensional at the same time.
Akzeptierte Antwort
Stephen23
am 1 Feb. 2020
>> F = @(varargin)isequal(varargin{:});
>> F(2,3,4)
ans = 0
>> F(2,2,2)
ans = 1
>> F(5,6,7,8,9,2)
ans = 0
>> F(6,6,6,6,6,6,6,6)
ans = 1
0 Kommentare
Weitere Antworten (3)
Guillaume
am 31 Jan. 2020
Assuming your input is a vector:
all(diff(yourvector) == 0)
is what you want:
>> all(diff([2, 3, 4]) == 0)
ans =
logical
0
>> all(diff([2, 2, 2]) == 0)
ans =
logical
1
>> all(diff([5, 6, 7, 8, 9, 2]) == 0)
ans =
logical
0
>> all(diff([6, 6, 6, 6, 6, 6, 6, 6]) == 0)
ans =
logical
1
0 Kommentare
Rik
am 1 Feb. 2020
Another of the many strategies: count the number of elements a unique() call gives you. (when I'm back at a computer I'll test the performance of the answers so far)
F=@(varargin) 1==numel(unique([varargin{:}]));
0 Kommentare
GeeTwo
am 27 Dez. 2022
Bearbeitet: GeeTwo
am 27 Dez. 2022
With a vector of singles or doubles, you could use ~std([a b c d]). If all the values are the same, the standard deviation will be zero, so the negation will be true.
Similar to star strider's function:
F=@(v)~std(v);
If you really want all of the inputs as separate arguments (again, singles or doubles),
function ans = allEqual(varargin)
~std(cell2mat(varargin));
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!