help-octave
[Top][All Lists]

## Arrays: Multidimensional manipulating

 From: John W. Eaton Subject: Arrays: Multidimensional manipulating Date: Tue, 12 Apr 2011 12:46:29 -0400

```On 12-Apr-2011, dirac wrote:

| Hi there,
|
| I am having problems with a multidimensional array at the moment and have
| looked online to see similar problems...
|
| I basically have an equation
|
| p = exp(-1*(a*(|k|^b)))
|
| which I write:
|
| p = exp(-1*abs(k).^b).
|
| This is simple enough so far; if I want to vary a, k, and b, I could use for
| loops or I could do it using a multidimensional array (what I want to do as
| I assume it'll be faster). I started by defining vectors for a, b, and k:
|
| k = [-20:0.05:20];
| b = [0:0.01:2];
| a = [0:0.001:0.01];
|
|
| and then tried to find p for all of the possible values of a, b and k and
| store in an array of three dimansion; I tried this:
|
| p = zeros(length(a),length(k),length(b));   %allocating memory
|
| p = exp(-1.*a.*(abs(k)).^b)
|
| Which doesn't seem to work.
| Is this something that you can do with Octave or will I have to use for
| loops?

a = 0:0.001:0.01;
k = -20:0.05:20;
b = 0:0.01:2;

na = numel (a);
nk = numel (k);
nb = numel (b);

A = repmat (reshape (a, na, 1, 1), [1, nk, nb]);
K = repmat (reshape (k, 1, nk, 1), [na, 1, nb]);
B = repmat (reshape (b, 1, 1, nb), [na, nk, 1]);

P = exp (-A.*(abs (K)).^B);

ploop = zeros (na, nk, nb);
for ii = 1:na
for jj = 1:nk
for kk = 1:nb
ploop(ii,jj,kk) = exp (-a(ii).*(abs (k(jj))).^b(kk));
endfor
endfor
endfor

absdiff = ploop - P;
max (absdiff(:))

jwe

```