help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

operations on binary matrices


From: mmasny
Subject: operations on binary matrices
Date: Thu, 24 Nov 2011 14:51:39 -0800 (PST)

Hello.

I rarely use Octave so I'm not very good at optimizing the code. I know that
it's a bad idea to use loops in Octave but I have too little knowledge not
to use them. Could you help me with my two problems?

I would like to write two programs. The first one is supposed to multiply
two square binary matrices. I want to multiply them in a way simlilar to the
way we multiply matrices usually, except I take the sum of elements to be
the logical disjunction and the product to be the logical conjunction. My
code is the following:

function wyn = zloz(A, B, n)

wyn = zeros(n);

for i = 1:n
for j = 1:n
for k = 1:n

if (A(i,k) & B(k,j))
   wyn(i,j) = 1;
   break;
endif

endfor
endfor
endfor

endfunction 

The second program is supposed to compare matrices in terms of the following
partial order: A<=B iff (A(i,j)==1 implies B(i,j)==1). Again, I'm using
loops:

function wyn = mniejsze(A,B,n)

for i=1:n
for j=1:n

if ( A(i,j) & (~B(i,j)) )
   wyn = 0;
   return;
endif

endfor
endfor

wyn = 1;

endfunction

Is it possible to make this significantly more efficient? 

Best regards,

Michał Masny

--
View this message in context: 
http://octave.1599824.n4.nabble.com/operations-on-binary-matrices-tp4105860p4105860.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]