Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

if in while loop not working - write combinations with unique proportions from input matrix to new matrix

1 vue (au cours des 30 derniers jours)
Kid
Kid le 6 Fév 2014
Clôturé : MATLAB Answer Bot le 20 Août 2021
I want to add the RGB-combinations (row in matrix P) with unique proportions (gcd=1) as a new row at the bottom of matrix Q. The code I have now (see below) isn't working though. Matlab doesn't create a new matrix Q.
N = 4;
R = 0:1:2^N-1;
R = transpose(R);
G = R;
B = R;
P = allcomb(R,G,B);
[m,n] = size(P);
i = 1;
while i <= m
i = i+1;
if gcd(P(i,1),gcd(P(i,2),P(i,3))) == 1
Q(end,:) = P(i,:);
end
  2 commentaires
dpb
dpb le 6 Fév 2014
Modifié(e) : dpb le 6 Fév 2014
A) IS there an existant Q? If so, it must have been created prior to the above code snippet.
B) If Q does exist, then
Q(end,:) = ...
replaces the last row in it rather than augmenting it. Write either
Q(end+1,:) = RHS;
or
Q=[Q; RHS];
Kid
Kid le 6 Fév 2014
Already fixed the problem with the help of someone on another forum, the code is now:
N = 5;
R = 0:1:2^N-1;
R = transpose(R);
G = R;
B = R;
P = allcomb(R,G,B);
Q = [];
s = 2^(3*N);
i = 1;
while i <= s
if gcd(P(i,1),gcd(P(i,2),P(i,3))) == 1
Q(end+1,:) = P(i,:);
end
i = i+1;
end

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by