Classification result of matrix row multiplication
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alice Zurock
le 22 Mar 2020
Modifié(e) : Alice Zurock
le 23 Mar 2020
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...
0 commentaires
Réponse acceptée
Sriram Tadavarty
le 22 Mar 2020
Modifié(e) : Sriram Tadavarty
le 23 Mar 2020
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 commentaires
Sriram Tadavarty
le 23 Mar 2020
Modifié(e) : Sriram Tadavarty
le 23 Mar 2020
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
Sriram Tadavarty
le 23 Mar 2020
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
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!