## Matlab should have give array but it gives a number. How to fix?

### Ege Tunç (view profile)

on 16 Mar 2019
Latest activity Edited by Jan

on 18 Mar 2019

### Jan (view profile)

I have a code to calculate some lambda values then uses these values with our r values to find d values. However, these values are 1x21 array but the given answer is just a number why? and how to fix this?
U=[10 9.7 9.4 9.1 8.8 8.5 8.2 7.9 7.6 7.3 7.0 6.7 6.4 6.1 5.8 5.5 5.2 4.9 4.6 4.3 4.0];
h=6.625.*10.^(-34);
m=9.109.*10.^(-31);
e=1.602.*10.^(-19);
lambda=h./sqrt(2.*m.*e.*U.*1000);
R=65.*10.^(-3);
r1i=[0.7 0.75 0.8 0.85 0.9 0.9 1.05 1.05 1.1 1.1 1.1 1.15 1.15 1.10 1.10 1.10 1.10 1.15 1.10 1.15 1.2];
r1o=[1.2 1.3 1.35 1.4 1.4 1.45 1.5 1.55 1.55 1.6 1.6 1.67 1.67 1.75 1.75 1.8 1.9 1.9 2 2.05 2.1];
r2i=[0.75 0.75 0.85 0.95 1 1 1.05 1.1 1.1 1.1 1.1 1.15 1.2 1.2 1.25 1.25 1.25 1.3 1.3 1.3 1.35];
r2o=[1.25 1.3 1.4 1.4 1.45 1.45 1.5 1.55 1.6 1.65 1.65 1.7 1.7 1.75 1.85 1.9 1.9 1.95 2 2.05 2.1];
r3i=[0.7 0.75 0.75 0.8 0.85 0.85 0.9 0.95 1 1 1 1 1.05 1.05 1.05 1.1 1.1 1.15 1.15 1.15 1.2];
r3o=[1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.5 1.55 1.6 1.6 1.65 1.7 1.7 1.75 1.8 1.85 1.9 1.95 2 2.05];
d1i=(2*R.*lambda)/(10.^(-2).*r1i);
d1o=(2*R.*lambda)/(10.^(-2).*r1o);
d2i=(2*R.*lambda)/(10.^(-2).*r2i);
d2o=(2*R.*lambda)/(10.^(-2).*r2o);
d3i=(2*R.*lambda)/(10.^(-2).*r3i);
d3o=(2*R.*lambda)/(10.^(-2).*r3o);
dinner=(d1i+d2i+d3i)/3
douter=(d1o+d2o+d3o)/3

R2018b

on 16 Mar 2019
Edited by Jan

### Jan (view profile)

on 18 Mar 2019

Use the elementwise division:
d1i=(2*R.*lambda) ./ (10.^(-2).*r1i);
% ^
You have used the elementwise operations exhaustively, even if they are not needed, because the operands are scalars only. But at the only location, where it is needed, the matrix division / is applied. I'd use:
d1i = 2 * R * lambda ./ (1e-2 * r1i);
% or even nicer:
d1i = 200 * R * lambda ./ r1i;
By the way, compare
h=6.625.*10.^(-34);
with
h = 6.625e-34;
The first is an expensive power operation, while the second is a cheap constant - and nicer. Then your code becomes:
U = [10 9.7 9.4 9.1 8.8 8.5 8.2 7.9 7.6 7.3 7.0 6.7 6.4 6.1 5.8 5.5 5.2 4.9 4.6 4.3 4.0];
h = 6.625e-34;
m = 9.109e-31;
e = 1.602e-19;
lambda = h ./ sqrt(2 * m * e * U * 1000);
R = 65e-3;
ri = [0.7 0.75 0.8 0.85 0.9 0.9 1.05 1.05 1.1 1.1 1.1 1.15 1.15 1.10 1.10 1.10 1.10 1.15 1.10 1.15 1.2; ...
0.75 0.75 0.85 0.95 1 1 1.05 1.1 1.1 1.1 1.1 1.15 1.2 1.2 1.25 1.25 1.25 1.3 1.3 1.3 1.35; ...
0.7 0.75 0.75 0.8 0.85 0.85 0.9 0.95 1 1 1 1 1.05 1.05 1.05 1.1 1.1 1.15 1.15 1.15 1.2];
ro = [1.2 1.3 1.35 1.4 1.4 1.45 1.5 1.55 1.55 1.6 1.6 1.67 1.67 1.75 1.75 1.8 1.9 1.9 2 2.05 2.1; ...
1.25 1.3 1.4 1.4 1.45 1.45 1.5 1.55 1.6 1.65 1.65 1.7 1.7 1.75 1.85 1.9 1.9 1.95 2 2.05 2.1; ...
1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.5 1.55 1.6 1.6 1.65 1.7 1.7 1.75 1.8 1.85 1.9 1.95 2 2.05];
di = 200 * R * lambda ./ ri; % Auto-expand, >= R2016b
do = 200 * R * lambda ./ ro; % Auto-expand, >= R2016b
dinner = mean(di, 1)
douter = mean(do, 1)
Less chances for typos.

Ege Tunç

### Ege Tunç (view profile)

on 16 Mar 2019
later, we found the dot issue, Thanks :)
Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 17 Mar 2019
Ege Tunç: you need to learn the difference between array and matrix operations: