Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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 Comments
dpb
dpb on 6 Feb 2014
Edited: dpb on 6 Feb 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 on 6 Feb 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

Answers (0)

Community Treasure Hunt

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

Start Hunting!