Your answer is one click away!

fR3ZNO February 2016
### Matlab - calculating condition numbers for hilbert matrices

Okay, first off I feel silly for asking this question since it seems like the answer should be simple but I can't figure it out.

I have a vector n:

```
n=[2:13]
```

I would like to pass this vector through elementwise so that it returns another vector with the calculated values:

```
condition=cond(hilb(n))
```

hilb(n) returns a Hilbert matrix with the dimension n. cond() then calculates the condition number, which is a scalar value. Currently, Matlab is returning condition as a single value using the first value from n, which is 2 instead of a vector that is equal in length to n.

I am aware of using .* , sin.() and other commands to compute things elementwise, but I can't find how to do this with a function such as this.

You can't pass a vector to `hilb`

like that, unfortunately. What you should do is use a loop, or simpler, `arrayfun`

:

```
condition = arrayfun(@(x) cond(hilb(x)), n)
```

Note that `arrayfun`

is just a disguised loop, so it offers no performance benefit compared to explicitly writing the loop.

```
condition = zeros(1,numel(n));
for ii = 1:numel(n)
condition(ii) = cond(hilb(n(ii)));
end
```

Keep in mind that `hilb`

is an ill-conditioned matrix, thus your values will get very high (`cond(hilb(13))=8.3042e+19`

. This means your resulting vector will look like: `1.0e+19* 0.0000 0.0000 ... 0.0017 8.3042`

. If you use `format short e`

, then you'll get to see the values of each individual element: `1.9281e+01 5.2406e+02 ... 8.3042e+19`

.

Asked in February 2016

Viewed 1,094 times

Voted 4

Answered 1 times

Viewed 1,094 times

Voted 4

Answered 1 times