Classification result of matrix row multiplication

1- Is there any less than -1 (<-1 )or bigger than (>1) Result1 or Result2, If there is ...-3, -2 ,2,3,4... Result1 or Result2 say that this row is BAD, Now print this row as a BAD(Because I need to now how many BAD row are there?).
2- If Result 1 and Result 2 is between: -1, 0 or 1, Now print this row as a GOOD(Because I need to now how many GOOD row are there?).
3- If Result 1 and Result 2 is: -1 or 0, Now print this row as a VERY GOOD(Because I need to now how many VERY GOOD row are there?).
And I need to check for that Length 4,5,6..... N matrixes.
PS For length 4 I need to look at 3 times circular shift because 4. circular shift same as original row. Length 5, need to look at 4 times circular shift, so on...

 Accepted Answer

Hi Alice,
Provided you have a matrix as defined for length N. Here is the code that would work for length equal to 2.
matrix = [1 1 1; 1 1 -1; 1 -1 -1; 1 -1 1; -1 1 1; -1 1 -1; -1 -1 -1; -1 -1 1];
n = size(matrix,2);
% Use a for loop
for i = 1:size(matrix,1)
tmpMat = matrix(i,:); % Assigns each row of the matrix
for j = 1:n-1
circMat = circshift(tmpMat,-j); % Perform circular shift
r(i,j) = sum(tmpMat.*circMat); % For each row and circular shift
end
% Perform display checks
if any(r(i,:) < -1) || any(r(i,:)>1)
disp('BAD')
end
if all(r(i,:) >= -1) && all(r(i,:) <= 1)
disp('GOOD')
end
if all(r(i,:) >= -1) && all(r(i,:) <= 0)
disp('VERY GOOD')
end
end
Hope this helps.
Regards,
Sriram

9 Comments

It exactly does what you have asked for. Did you get any errors? I think the bottom disp used ** instead of &&, if you used it. Is the same you are pointing?
Hey Alice, You shouldn't perform both the codes at the same time. I suggested you could replace the bottom code with the code in the first part. But, didn't indicate to run both. Can you just run only the first part of code and then execute? If you wanted to get a similar result as first part of code.. then replace the if else statements with the bottom code.
Hope this clears.
Hi Alice, i have updated the answer to make it straight forward. Please check the code in answer and let me know
You could assign variables before the for loop, like
badcount = 0;
goodcount = 0;
vgoodcount = 0;
% start for loop
% in the if condition related to bad, add this
badcount = badcount+1;
% in the if condition, related to good, add this
goodcount = goodcount+1;
% in the if condition, related to very good, add this
vgoodcount = vgoodcount+1;
Hope this helps.
Please use the updated answer and that should help. If you use the code with r(i,1) and r(i,2) you may get unexpected results. SInce, it only checks the first two elements.
Just replace the if..end statements in the above code with that of statements present in the answer
Hi Alice, Please do not delete the comments which you placed. If you delete the comments and then what ever comments are there, they lose it's meaning, of why is it said as such.
For the latest comment, you need to look over result output in the workspace. I got 6 Good rows and no Very good rows. So, the very good rows is wrong, need to have minor update as such
if all(result(i,:) >= -1) && all(result(i,:) <= 0)
disp(['Very Good' num2str(i)])
end
Update this in the code you placed in the comment.
This would solve
Hi Alice, you are not updating the counter variable, in the latest code, Update this and it would solve. If the answer is helpful, do accept it
bad = 0;
good = 0;
verygood = 0;
len=3;
N = 2^len;
binary = de2bi(N-1:-1:0); % Convert decimal numbers in to binary
binary(binary==0) = -1; % Replace the values of 0 with -1
n = size(binary,2);
for i = 1:size(binary,1)
MyMatrix = binary(i,:); % Assigns each row of the matrix
for j = 1:n-1
circMat = circshift(MyMatrix,-j); % Perform circular shift
result(i,j) = sum(MyMatrix.*circMat); % For each row and circular shift
end
if any(result(i,:) < -1) || any(result(i,:) > 1)
disp(['Bad' num2str(i)])
bad = bad+1; % Incremet the bad variable
end
if all(result(i,:) >= -1) && all(result(i,:) <= 1)
disp(['Good' num2str(i)])
good = good+1; % Increment the good variable
end
if all(result(i,:) >= -1) && all(result(i,:) <= 0)
disp(['Very Good' num2str(i)])
verygood = verygood+1; % Increment verygood variable
end
end
Hope this helps.
Regards,
Sriram
Yes, i did. I get 6 good rows and 6 very good rows. Can you clear the variables and try the code?
The output i get
Bad1
Good2
Very Good2
Good3
Very Good3
Good4
Very Good4
Good5
Very Good5
Good6
Very Good6
Good7
Very Good7
Bad8

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!