How to do this
1 次查看(过去 30 天)
显示 更早的评论
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);
M(M>1)=-1;
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
if -1 < c < 1
if M3(:,:) > 0
M3 = -1
else M3(:,:) < 0
M3 = 1
end
elseif 0 < c < 2
I need to:You will need to then determine the probability of spin change. Use that probabilities in the table given
based on:Neighbors with Similar Spin 1 2 3 4 5 Prob. 0.98 0.85 0.50 0.15 0.02
This means that if c = 0 100% chance that the element in the same position as c in M3 sill change from 1 to -1 or -1 to 1
if c = 1, a 98% chance it will change, 2 an 85% chance, 3 a 50% cahnce, 4 a 15% chance and 5 a 2% chance
how do i do this
3 个评论
Jan
2018-6-18
编辑:Jan
2018-6-18
@Hunter Herrenkohl: No, -1 < c < 1 cannot do, what you want. Did you read Stephen's comment?
-1 < c is either true or false, which is equivalent to 1 or 0. Afterwards you compare this 1 0r 0 by < 1. This is valid Matlab code, but a weird formulation. Do you mean this instead:
|-1 < c & c < 1|
?
回答(1 个)
Jan
2018-6-18
You apply a comparison to a matrix and use it as condition to an if command. But if requires a scalar condition. Therefore Matlab inserts an all() implicitly. To be exact:
if all(condition(:)) && ~isempty(condition)
You want something else - at least I guess this.
cond1 = (-1 < c & c < 1); % Not -1 < c < 1 !!!
cond2 = M3 > 0;
cond3 = M3 < 0;
M3(cond1 & cond2) = -1;
M3(cond1 & cond3) = 1;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!