2

I have quite a basic question but I don't know how to do it. EDIT: Sorry for writing it unclearly I hope I can clarify it.

I want to write an if-else statement as a vector in linear algebra. However, this vector is the result of an if-else-statement of the form:

if b>1
then a=0
else a=c

I try to replicate it in the following form: I have a vector three vectors with length $n$.

Vector $b$'s elements are between 0 and 2 for example like this:
$b=\begin{pmatrix} 2 \\ 0.5 \\ \vdots \\ 1.5 \\ \end{pmatrix} $

Vector $C$'s elements are between 0 and 1 for example like this

$c=\begin{pmatrix} 0.1 \\ 0.2 \\ \vdots \\ 0.8 \\ \end{pmatrix} $

Now I want to set up an equation to construct vector $a$ with elements $a_1$ to $a_n$ so that

$a=\begin{pmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \\ \end{pmatrix}$.

I want to set all the enteries $a_i$ to 0 if $b_i$ is below a specific value e.g. 1. Otherwise I want set $a_i$ to $c_i$. So in this example: $a=\begin{pmatrix} 0 \\ 0.2 \\ \vdots \\ 0.8 \\ \end{pmatrix}$.

Linearily I would write it like this $a(i) = \begin{cases} 0 & \text{if } b(i)<1 \\ c(i) & \text{otherwise.} \end{cases}$ with $a, b,c,$ being functions that contain the same values of my example.

But how can I define such a vector? I want to to use this vector in a later multiplication.

I know that the questions is very familiar to these questions but they do not look at linear algebra:

Thank you so much!

Karl A
  • 43
  • Are you allowed to use step function? Also: what programming language are you using? There might be a simple way to do it – Andrei Oct 09 '17 at 17:38
  • This is less a question of mathematics per se and more one of engineering. – Steven Stadnicki Oct 09 '17 at 17:48
  • I hope my clarifications make it much clearer. It is not really about the programming language but more about the linear algebra so I wrote some pseudocode for the example – Karl A Oct 10 '17 at 07:46

2 Answers2

1

I would define a square matrix S with zeros everywhere except on the diagonal where we put a $1$ if the corresponding element in $b$ is greater than $1$ and $0$ otherwise.

The diagonal may be obtained as the vector $(b\geq 1)$, assuming you use a programming language where false is zero and true is one.

Alternatively, if all of $b$ elements are positive, you can obtain the diagonal as follows:

$S_{ii} = ceil(floor(b_i) / (b_i+0.5))$

where $floor(x)$ yields the largest integer equal or smaller than $x$ and $ceil(x)$ the smallest integer equal or larger than $x$. The +0.5 is to avoid division by zero error.

Then compute $a=Sc$.

Elsa
  • 908
  • Thank you very much for your answer! I did already code it so it is purely about writing it up. so how would you write the vector in "math". I will have to look into the answer in more detail to understand it, but thanks a lot ! – Karl A Oct 10 '17 at 21:51
0

I guess you cannot get the if-then-else construct in linear algebra intended as choice of the type "if $x_i>a$ ..." because this would introduce discontinuities, but all the linear operator (on finite dimension real spaces) are continuous. A try could be to encode true and false as $(0,1)$ and $(1,0)$ or to use boolean vector fields, i.e. $\mathbb{F}_2^n$.

Tancredi
  • 1,522