i want matrix E == matrix B(i want correct answer)

I have tried to run this code many times , even changed the equation , values but i am not able to get the correct answer , here A given is before its transpose of glcm
% Convert to grayscale if necessary
%if size(img, 3) > 1
%img = rgb2gray(img);
%end
I = [0 0 1 1;
0 0 1 1;
0 2 2 2;
2 2 3 3];
A = [2 2 1 0;
0 2 0 0;
0 0 3 1;
0 0 0 1];
D = A';
E = A + D;
E
%glcms = graycomatrix(I);
%glcms
m = 5;
%n = 4;
count = 0;
%count1 = 0;
n = 3;
o = 4;
for i = 1 : 4
for j = 1:4
for l = 1 : 4
for k = 1 : 4
x = I(i,1);
y = I(i,3-1);
a = I(i,3+1-1);
b = I(i,2+2-1);
if (I(i,j) && I(i,n-j)) == (I(i,n-k) && I(i, n+1-k))
%disp('Entered the control');
% count = count+ 1;
%end
end
end
B(i,j) = count;
end
%B(i,j) = count;
%end
%end
end
%if (E == B)
% disp('Correct Answer');
%else
% disp('Not Correct Answer');
%end

1 Comment

a = I(i,3+1-1);
b = I(i,2+2-1);
Those always evaluate to the same location, a = I(i, 3) and b = I(i, 3)

Sign in to comment.

 Accepted Answer

E==B is going to do an element-wise comparison, returning a matrix the same size as E and B with logicals (0's, 1's) where the elements in the same positions are equal, sounds like you want a simple true/false if E and B are the same
if you're wanting to build a condition on the entire matrices being equivalent, then you probably want to use:
if isequal(E,B)
...
end
isequal() will output a single true/false value
you could also use all(all(E==B)) to reduce the element-wise comparison to a single true/false

7 Comments

I am sorry but can you give me the exact logical equation which satisfies the condition matrix E == matrix B , i want code : %glcms = graycomatrix(I);
%glcms
m = 5;
%n = 4;
count = 0;
%count1 = 0;
n = 3;
o = 4;
for i = 1 : 4
for j = 1:4
for l = 1 : 4
for k = 1 : 4
x = I(i,1);
y = I(i,3-1);
a = I(i,3+1-1);
b = I(i,2+2-1);
if (I(i,j) && I(i,n-j)) == (I(i,n-k) && I(i, n+1-k))
i have tried my level best , whatever conditions and equations i know , with the values i have tried , i only got index is out of bounds or matrix E is not equal to matrix B. so , please anyone i sincerely request you to show me the result
i got index out of bound issues with this line:
if (I(i,j) && I(i,n-j)) == (I(i,n-k) && I(i, n+1-k))
...not quite sure what you're trying to do here but seems kinda confusing with all the logicals (&&,==)
if you want to test for matrix E and B being equivalent: isequal(E,B) returns 1 if equal 0 if not
A=magic(3)
A = 3x3
8 1 6 3 5 7 4 9 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B=randi(9,3)
B = 3x3
7 1 9 6 4 7 8 1 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A==B
ans = 3x3 logical array
0 1 0 0 0 1 0 0 0
A=magic(3);
B=magic(5);
isequal(A,B)
ans = logical
0
A==B
Arrays have incompatible sizes for this operation.
isequal() can handle matrices of different sizes whereas == cannot
maybe we're miscommunicating and you're really wanting help with the process to generate matrix B such that it will be equal to E?
no you're right.
% Define the original matrix I
I = [0 0 1 1;
0 0 1 1;
0 2 2 2;
2 2 3 3];
% Define matrices A and I
A = [2 2 1 0;
0 2 0 0;
0 0 3 1;
0 0 0 1];
% Calculate matrix D
D = A';
% Calculate matrix E
E = A + D;
E
% Initialize matrix B
B = zeros(size(I));
% Loop through each element of I
for i = 1:size(I, 1)
for j = 1:size(I, 2)
% Modify B based on conditions
% Replace with specific values according to the desired pattern
% Here we're setting B(i,j) to a value based on the corresponding element in E
B(i,j) = E(i,j);
end
end
B
% Check if matrices E and B are equal
if isequal(E, B)
disp('Matrices E and B are equal.');
else
disp('Matrices E and B are not equal.');
end
%Matrices E and B are equal.
you're very welcome...the latest version is much easier to follow too, nice structure and commeting as well, bravo!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 11 May 2024

Commented:

on 12 May 2024

Community Treasure Hunt

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

Start Hunting!