help-octave
[Top][All Lists]
Advanced

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

Re: re-assign special values in matrix


From: James Sherman Jr.
Subject: Re: re-assign special values in matrix
Date: Fri, 12 Nov 2010 01:23:23 -0500

On Fri, Nov 12, 2010 at 1:04 AM, Isak Delberth Davids <address@hidden> wrote:
Here comes a lay-man,

I intend to avoid dividing by zero, by re-assigning a different value to a entry that is zero in a divisor matrix. In the following code I want to search for that entry of A which equals zero (being A(7) in this case), and give it a value that is greater then zero but smaller than all other non-zero entries (which is A(3)=9 in this case). I am trying a if-statement as can be seen --- which seems WRONG. Where should I fix?

% code begin
clear;clc;

a = [3; 2; 1; 3; 4; 5; 1]
A = [12; 11; 9; 13; 67; 44; 0]

min(A(A~=0)) = A(A==0) % trying to set the zero values in divisor matrix to the min non-zero entry?

B = a./A
% code begin


Hi,

You have it almost what you want to do.  The thing is, that unlike mathematics, whether you put things on the left hand side or right hand side of the equals sign matters in octave (and in programming in general).  So, for example, let
A = [1;2]
B = [3;4]
then, executing
A = B
would put all the values of B into A and after this command, B would be unchanged and A would be equal to [3;4].
On the other hand, if you did 
A = [1;2]
B = [3;4]
then did
B = A
You would have A unchanged and B would be equal to [1;2].

So, for your example, you would want:
A(A==0) = min(A(A~=0));
should do the trick.
 
Hope this helps.

reply via email to

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