Clear Filters
Clear Filters

How do i do this

1 view (last 30 days)
Hunter Herrenkohl
Hunter Herrenkohl on 18 Jun 2018
Commented: Hunter Herrenkohl on 18 Jun 2018
I have
R = input('Please enter a number of rows for the matrix')
C = input('Please enter a number of columns for the matrix')
M = randi(2, R, C)
for M2 = 1:numel(M)
if M(M2) > 1
M(M2) = -1
end
end
M3 = M
for MN = 1:numel(M3)
I need to: Part c: You will need to use a loop to check the neighbors of each element and then count how many agree with the current element you are on. You will need to check the elements to the left, right, above and below. You will probably need to create another matrix to keep track of the similar spin count.
How do i do this?
  2 Comments
John D'Errico
John D'Errico on 18 Jun 2018
Define "agree". Is that a synonym for "is the same as" or "isequal to"?
Hunter Herrenkohl
Hunter Herrenkohl on 18 Jun 2018
equal to, i have a RxC matrix (the dimensions indicated from the user) filled with 1s and -1s. I have to find out how many are equal to their neighbors and how many of their neighbors they are equal to in order to figure out if they will change or not

Sign in to comment.

Accepted Answer

Ankita Bansal
Ankita Bansal on 18 Jun 2018
Hi, I am assuming that you are trying to compare M3(i,j) with it's adjacent elements and you want to store the number of times M3(i,j) matches with adjacent elements in a new variable c(i,j). You can do this by creating a new matrix of size (R+2,C+2).
R = input('Please enter a number of rows for the matrix');
C = input('Please enter a number of columns for the matrix');
M = randi(2, R, C);
M(M>1)=-1; % instead of "for M2 = 1:numel(M) if M(M2) > 1 M(M2) = -1 end end"
M3 = M;
M4=NaN(R+2,C+2);
M4(2:end-1,2:end-1)=M3;
c=zeros(R,C);
for i = 2:R+1
for j=2:C+1
arr = [M4(i-1,j) M4(i+1,j) M4(i,j+1) M4(i,j-1)];
c(i-1,j-1)=nnz(ismember(arr,M4(i,j)));
end
end
  1 Comment
Hunter Herrenkohl
Hunter Herrenkohl on 18 Jun 2018
Thank you!! This works awesome

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!