【发布时间】:2014-04-28 13:16:38
【问题描述】:
我有一个关于 Matlab 中的短路运算和空矩阵的问题。我将短路操作放在我的 if 语句中。但有时我有空矩阵,因此它们不能转换为逻辑标量值。
我已阅读此文档。 http://www.mathworks.com/help/matlab/math/empty-matrices-scalars-and-vectors.html 但我无法控制空矩阵和逻辑值的顺序。
在我看来,我想要像“[] || 1 to be 1”这样的东西。
感谢您的帮助!
这是原始代码。这是在网络分析中进化选民模型的代码。
clear
clc
load('karate.mat');
rng shuffle
n = length(A);
rho = 0.5;
alpha = 0.2; %rewiring probability
%rewire to the same
t = 1;
B{t} = A; %temperal network
D{t} = zeros(n,n); % the discordant edges
S = zeros(n,1); % the infected nodes (set to zero if not infected)
SS = rand(n,1);
S(SS<rho) = 1;
for i = 1:n
for j = 1:n
if abs(S(i)-S(j)) > 0.5
if B{t}(i,j) == 1
D{t}(i,j) = 1;
end
end
end
end
flag=1;
t=1;
while(flag)
t=t+1;
for i=1:n
for j=1:n
if(D{t-1}(i,j)>0.1)
ii=i;
jj=j;
neighB=find(B{t-1}(ii,:));
neighB1=neighB(S(neighB)==1);
neighB0=neighB(S(neighB)==0);
kB=length(neighB);
kB1=length(neighB1);
kB0=kB-kB1;
neighD=find(D{t-1}(ii,:));
neighD1=neighD(S(neighD)==1);
neighD0=neighD(S(neighD)==0);
kD=length(neighD);
kD1=length(neighD1);
kD0=kD-kD1;
if S(ii)==1
if(rand(1)<1-alpha) %adopting
S(ii)=0;
if(kD1>0)
D{t}(ii,neighD1)=1;
D{t}(neighD1,ii)=1;
end
if(kD0>0)
D{t}(ii,neighD0)=0;
D{t}(neighD0,ii)=0;
end
elseif((max(S(find((B{t-1}(ii,1:(ii-1)))==0)))==1) || (max(S(find((B{t-1}(ii,(ii+1):end))==0)))==1)) %rewire could happen if there exists a node having the same status and is not a neighbor
B{t}(ii,jj)=0;
% have a problem to compare [] and 1 here
% Operands to the || and && operators must be convertible to logical scalar values.
F=find(S(find((B{t-1}(ii,1:(ii-1)))==0)))==1 && find(S((find(B{t-1}(ii,(ii+1):end))==0)))==1; %not sure here
C=randperm(length(F)); %not sure here
B{t}(ii,S((C(1))))=1; %not sure here
D{t}(ii,neighD1)=0;
D{t}(neighD1,ii)=0;
D{t}(ii,neighD0)=1;
D{t}(neighD0,ii)=1;
else
S(ii)=0; %if cant find other nodes to rewire, then change the status
D{t}(ii,neighD0)=0;
D{t}(neighD0,ii)=0;
D{t}(ii,neighD1)=1;
D{t}(neighD1,ii)=1;
end
end
if S(ii)==0
if(rand(1)<1-alpha)
S(ii)=1;
if(kD1>0)
D{t}(ii,neighD1)=0;
D{t}(neighD1,ii)=0;
end
if(kD0>0)
D{t}(ii,neighD0)=1;
D{t}(neighD0,ii)=1;
end
elseif((min(S(find(B{t-1}(ii,1:(ii-1)))==0))==0) || (min(S(find(B{t-1}(ii,(ii+1):end))==0))==0)) %rewire could happen if there exists a node having the same status and is not a neighbor
B{t}(ii,jj)=0;
F=find(S(find(B{t-1}(ii,1:(ii-1)))==0))==0 && find(S(find(B{t-1}(ii,(ii+1):end))==0))==0; %not sure here
C=randperm(length(F)); %not sure here
B{t}(ii,S((C(1))))=1; %not sure here
D{t}(ii,neighD0)=0;
D{t}(neighD0,ii)=0;
D{t}(ii,neighD1)=1;
D{t}(neighD1,ii)=1;
else
S(ii)=1; %if cant find other nodes to rewire, then change the status
D{t}(ii,neighD0)=1;
D{t}(neighD0,ii)=1;
D{t}(ii,neighD1)=0;
D{t}(neighD1,ii)=0;
end
end
end
end
end
if(nnz(D{t})==0)
flag=0;
disp(t);
end
end
nnz(S)/length(A)
disp(t)
【问题讨论】:
-
使用
isempty。像 -isempty(..) || 1 -
谢谢,但有时我的 [] 部分可能有 0 或 1。所以我不知道如何处理。我想要的只是如果结果不是真(1),那么它就是假的。不管是0还是[]。
-
“[] 部分”到底是什么?我假设您在某个向量或矩阵或某个标量上运行它?那么也许举一些例子并显示所需的输出?
-
(max(S(find((B(i,1:(i-1)))==0)))==1 and (max(S(find((B(i ,(i+1):end))==0)))==1)
-
谢谢 Divakar,这是我想检查的内容。我正在循环 i 那里。
标签: matlab matrix logical-operators