1 view (last 30 days)

Hi, I have the following matrix:

combs45 = nchoosek(1:45,2);

I want to change some of its rows to zero as follows:

%i=row number

combs45(1,:)=0 %i=1

combs45(2,:)=0 %i=1+1

combs45(45,:)=0 %i=1+44

%combs45(88,:) keep as it is ,i=1 +44 + 43

combs45(130,:) =0 %i=1+ 44 + 43 + 42

combs45(131,:)=0 %i=1 + 44 + 43 + 42 + 1

combs45(171,:)= 0 %i = 1 + 44 + 43 + 42 + 41

%combs45(211,:) keep, i=1 + 44 + 43 + 42 + 41 +40

combs45(250,:) = 0 %i= 1+ 44 + 43 + 42+ 41 + 40 + 39

combs45(251,:) = 0 %i=1 + 44 + 43 + 42 + 41 + 40 + 39 +1

and so on, until the end of the matrix.

I think the pattern is obvious in terms of what i is incremented by, also I want to keep the row unchanged when the number of steps can be exactly divided by 4.

Basically I don't want to pair together any of the elements inside the triplets (1,2,3), (4,5,6), ..., (43,44,45). Specifically, I don't want to pair 1 with 2 or 3, and 2 with 3, then I don't want to pair 4 with 5, etc, but I'm okay pairing 1 with 4, and 4 with 7, etc.

So I thought that in the 45 choose 2 matrix I want to first change the rows of those pairs (e.g., row1 is [1 2]) to 0 (so make row 1 [0 0]), and then remove the 0 elements from the matrix.

I wrote the following code but it's taking too long to run so I don't know if it's actually any good:

combs45=nchoosek(1:45,2);

i=1;

j=44;

count = 0;

while i<=45

while j>0

if mod(count,4)~=0

combs45(i,:)= 0;

count = count+1;

if mod(count,2)== 0

i=i+1;

combs45(i,:)= 0;

count = count+1;

else

i=i+j;

combs45(i,:)= 0;

count = count+1;

j = j-1;

end

else

count = count+1;

j = j-1;

i = i+j;

end

end

end

I'm wondering if there's a faster code I can run (or if there is a better pattern I can find and then adapt the code accordingly). Any help would be appreciated, thank you!

Ameer Hamza
on 26 Jun 2018

Edited: Ameer Hamza
on 26 Jun 2018

Here is my attempt to generate the pattern as described in your question, there might be more efficient ways.

combs45 = nchoosek(1:45,2);

i_pattern = 1; % first pattern value

startNumber = 44;

count = 2;

while i_pattern(end) < size(combs45, 1)

nextValue = 1;

if mod(count, 4) == 2

nextValue = i_pattern(count-1) + 1;

elseif mod(count, 4) == 3

nextValue = i_pattern(count-2) + startNumber;

startNumber = startNumber - 1;

else

nextValue = i_pattern(count-1) + startNumber;

startNumber = startNumber - 1;

end

i_pattern = [i_pattern nextValue];

count = count+1;

end

disp(i_pattern)

Columns 1 through 13

1 2 45 88 130 131 171 211 250 251 288 325 361

Columns 14 through 26

362 396 430 463 464 495 526 556 557 585 613 640 641

Columns 27 through 39

666 691 715 716 738 760 781 782 801 820 838 839 855

Columns 40 through 52

871 886 887 900 913 925 926 936 946 955 956 963 970

Columns 53 through 59

976 977 981 985 988 989 990

Then to set the required rows to zero

i_pattern(4:4:end) = []; % delete every 4th entry.

combs45(i_pattern, :) = 0;

I tried running your code and It appears to be wrong since the value of count exceeds beyond the length of combs45.

Ameer Hamza
on 26 Jun 2018

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582831

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582831

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582833

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582833

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582835

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582835

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582845

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582845

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582887

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582887

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582960

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/407533-how-can-i-change-the-rows-of-a-nchoosek-matrix-to-zero-according-to-a-specific-pattern-and-avoid-n#comment_582960

Sign in to comment.