Borrar filtros
Borrar filtros

How do i do this

1 visualización (últimos 30 días)
Hunter Herrenkohl
Hunter Herrenkohl el 18 de Jun. de 2018
Comentada: Hunter Herrenkohl el 18 de Jun. de 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 comentarios
John D'Errico
John D'Errico el 18 de Jun. de 2018
Define "agree". Is that a synonym for "is the same as" or "isequal to"?
Hunter Herrenkohl
Hunter Herrenkohl el 18 de Jun. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Ankita Bansal
Ankita Bansal el 18 de Jun. de 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 comentario
Hunter Herrenkohl
Hunter Herrenkohl el 18 de Jun. de 2018
Thank you!! This works awesome

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by