finding if number(iteration) is the sum of 2 numbers inside same array - matlab

4 visualizzazioni (ultimi 30 giorni)
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
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...

Accedi per commentare.

Risposte (3)

Sam Chak
Sam Chak il 23 Apr 2022
Modificato: Sam Chak il 23 Apr 2022
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

Jan
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
result = 1×6 logical array
0 0 0 1 1 1

Voss
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))
ans = 1×6 logical array
0 0 0 1 1 1
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
1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6
disp(X(nchoosek(1:numel(X),2))) % index into X using those indexes, i.e., get the elements from X
20 40 20 50 20 60 20 80 20 100 40 50 40 60 40 80 40 100 50 60 50 80 50 100 60 80 60 100 80 100
disp(sum(X(nchoosek(1:numel(X),2)),2)) % add each pair of elements
60 70 80 100 120 90 100 120 140 110 130 150 140 160 180
disp(ismember(X,sum(X(nchoosek(1:numel(X),2)),2))) % see which elements of X are in the summed-pairs array
0 0 0 1 1 1
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)))
1 1 0 1 1 1
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)))
1 1 1 1 1 1 1
(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.)

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by