finding if number(iteration) is the sum of 2 numbers inside same array - matlab
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
hello,
im trying to write a code that will check every number inside a 1 dimention array ( loop),
and check if the number can be summed by 2 numbers inside the array.
thanks in advance.
[EDITED] ill try to clarify.
given an array = [ 20 40 50 60 80 100]
if one of the element is the sum of 2 other element, print 1/ true
if it does not, than print 0/false
in this instance it should be true for 2 occurnaces, (20+40 = 60) and (20 + 80 = 100)
thanks in advance
1 Commento
Sam Chak
il 23 Apr 2022
Modificato: Sam Chak
il 23 Apr 2022
Can you define what the number cannot be summed by 2 numbers inside the array?
More specifically, is the problem related to the Fibonacci numbers?
If true, then it becomes a problem of checking whether each element in the array is a Finonacci number or not...
Risposte (3)
Sam Chak
il 23 Apr 2022
Modificato: Sam Chak
il 23 Apr 2022
Hi @Oren Savir
This is just a very simple example. You can turn it into a loop to check each element in the array.
% The array
X = [1 1 2; 3 5 8; 13 21 34]
X = transpose(X);
% Reshape array into vector
A = reshape(X, 1, [])
% Generate fibonacci sequence
B = fibonacci(1:length(A))
% Check true/false (1 means true, 0 means false)
tf = isequal(A, B)
X =
1 1 2
3 5 8
13 21 34
A =
1 1 2 3 5 8 13 21 34
B =
1 1 2 3 5 8 13 21 34
tf =
logical
1
0 Commenti
Jan
il 23 Apr 2022
Modificato: Jan
il 23 Apr 2022
x = [20 40 50 60 80 100];
n = numel(x); % Number of elements
index = nchoosek(1:n, 2); % Pairs of elements
sumx = sum(x(index), 2); % Sum of pairs
sumx = unique(sumx); % Cheaper to ignore multiple values
result = ismember(x, sumx) % TRUE if element is part of sumx
0 Commenti
Voss
il 23 Apr 2022
Modificato: Voss
il 23 Apr 2022
Here's something that may be close to what you want to do:
X = [20 40 50 60 80 100];
ismember(X,sum(X(nchoosek(1:numel(X),2)),2))
That tells you that 20, 40, and 50 cannot be made by adding two distinct elements in X, but 60, 80, and 100 can.
Breaking down that expression, step-by-step:
disp(nchoosek(1:numel(X),2)) % generate all pairs of distinct indexes into X
disp(X(nchoosek(1:numel(X),2))) % index into X using those indexes, i.e., get the elements from X
disp(sum(X(nchoosek(1:numel(X),2)),2)) % add each pair of elements
disp(ismember(X,sum(X(nchoosek(1:numel(X),2)),2))) % see which elements of X are in the summed-pairs array
Note that this method is not "the sum of two other elements"; it is "the sum of any two elements" (really, the sum of elements at any two indices). So if you had a zero in X, then every non-zero element would be counted because it is itself+0.
X = [20 40 0 60 80 100];
disp(ismember(X,sum(X(nchoosek(1:numel(X),2)),2)))
and if you had more than one 0 element, then all elements would always be counted:
X = [20 40 0 60 80 100 0];
disp(ismember(X,sum(X(nchoosek(1:numel(X),2)),2)))
(I point it out because it's not clear what your expected result would be in those cases, based on one example with no repeated elements and no zero elements.)
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!