How to binary sum columns in a binary matrix

Hello guys,
I hope you can help me, I'm currently studying the Hamming code in college, but it's only my second time using MatLab so i'm having some trouble answering this question:
"Calculate the minimum number of rows that you need to sum (XOR actually) on the Parity Test matrix so that you obtain a null vector, also indicate the row's index of the first set you found."
I made a rough translation of the question, but from my understading I need to sum(XOR) the values of each column, until I obtain a null vector.
In terms of coding this is what I have:
%2.6 - Parity Test Matrix
n = 255;
k = 247;
[H,G,n,k] = hammgen(8);
H=H'
And this gives me a huge binary matrix, let's assume that these few lines:
[1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
...
1 1 1 0 0 0 0 0]
The sum of these 4 vectors should give me:
[0 0 0 0 0 0 0 0]
I have no idea how to this, can you guys please lend me a hand ? I hope the question is not confusing but i'm totally lost. I tried to use the sum() fuction but i got [128 128 128 128] or something in return.
Thanks in advance.

 Akzeptierte Antwort

James Tursa
James Tursa am 19 Nov. 2018

1 Stimme

Just doing xor between succesive rows:
r = some row number
result = xor(H(r,:),H(r+1,:);
Can you wrap some code around this to get what you want?

6 Kommentare

Redfoxxie
Redfoxxie am 19 Nov. 2018
Hey James,
Thanks for your input, I was thinking of something close to that, but i'm guessing i need to include those lines in a for statement and I need the loop to stop when result = [0 0 0 0 0 0 0 0] (am I thinking correctly?), also I don't know how to do both of these in MatLab.
James Tursa
James Tursa am 19 Nov. 2018
Bearbeitet: James Tursa am 19 Nov. 2018
Yes, the intent was for you to wrap that in a for loop to get the result. E.g., something like this:
result = H(1,:);
for k=2,size(H,1)
if( some condition is met ) <-- you fill in this condition
break;
end
result = xor(result,H(k,:));
end
% add code here to see if you met the condition
Redfoxxie
Redfoxxie am 19 Nov. 2018
Actually there's no restraint on how to solve it, so anything goes. Is there a more direct ou simple approach ?
James Tursa
James Tursa am 19 Nov. 2018
The above code is straightforward and simple to understand, so I suggest you use this method first. There are ways to vectorize this to avoid the loop using functions like cumsum( ) and mod( ) etc, but I see no compelling reason for you to go that route at this point.
Redfoxxie
Redfoxxie am 19 Nov. 2018
Bearbeitet: Redfoxxie am 19 Nov. 2018
James, I tried the following:
n = 255;
k = 247;
[H,G,n,k] = hammgen(8);
H=H';
result = H(1,:)
for count=2:size(H,1)
result = xor(result,H(count,:));
if( all(result == 0))
disp('Im here')
break;
end
end
r = result
n_rows = count
This is the output:
result = 1 0 0 0 0 0 0 0
Im here
r = 0 0 0 0 0 0 0 0
count = 255
I'm guessing the code is running properly now, result has the proper vector for the first line of H, it enters the if condition when result has all 0 and the count was made up to 255 which is the total number of rows of the H matrix.
This is probably a characteristic of an Hamming Code, not sure though, but it seems the code is running as it should be, so thanks a lot James! If you notice any mistake on my part please let me know. Again, thanks.
James Tursa
James Tursa am 19 Nov. 2018
For readability, I would avoid using l for a variable name (looks too much like 1).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by