You could use the fact that $A^i(s,t)$ gives the number of different walks from $s$ to $t$ of exactly $i$ many steps. Thus, you could just compute $A^0, A^1, A^2, A^3, ...$ and keep the minimum exponent $i$ for which it is $A^i(s,t) > 0$, since such $i$ matches the distance. Here is an example that uses this fact:
% let B(s,t) keep the minimum power i with A^i(s,t) > 0
B=zeros(n,n);
for i=1:n
B=B+(B<=0).*(A^i>0).*i;
end
% set main diagonal to zero (optional, otherwise get the 'return time')
B(eye(size(B))~=0)=0;
Some comments on the above code: for a connected graph, you can also break the loop after $diam(G)$ many steps, i.e., when B contains no zeros. Further, one could avoid computing $A^i$ in each step and use something like $Apow = Apow * A$ within the loop. Also note that here the main diagonal is manually set to zeros in the end, since starting the loop at $i=0$ does not work without further modifications (one would also have to flag unset entries within $B$ by another number than $0$ then, e.g., by $-1$).
However, note that this is just an example of how one could compute the shortest paths in Matlab with a single loop and matrix operations only. Its computational effort (including the above optimizations) is something like $\mathcal{O}(diam(G)*M(n))$ with $M(n)$ the effort of a matrix multiplication, thus, something like $\mathcal{O}(diam(G)*n^3)$. This is far from optimal, and especially bad for large sparse graphs. I do not believe that there is an efficient pure-matrix-operative solution to this problem. For example, the dynamic programming approach of Dijkstra, cannot efficiently be expressed in matrix form.
Unfortunately, Matlab again and again tells the user to avoid for-loops, since they are so 'slow'. But this tale does only hold for Matlab's horrible implementation of for-loops! In a real programming language, for-loops are much much faster than in Matlab, and they are even the most efficient way to access the memory in an arbitrary order that does not follow the access pattern of a matrix operation.
Label all the vertices $V = \{v_1, \dots, v_n\}$. This formula is supposed to represent the average of all the shortest paths in the graph. Hence, given a node $v_1$, the average length of shortest paths emanating from $v_1$ is
$$
\frac{1}{n-1}\sum_{i\neq 1}d(v_1, v_i),
$$
since there are $n-1$ vertices distinct from $v_1$. Likewise, the average of shortest paths emanating from $v_k$ is
$$
\frac{1}{n-1}\sum_{i\neq k}d(v_k, v_i).
$$
So, if we wish to find the average of all shortest paths, we take the average of these:
$$
\frac{1}{n}\left(\frac{1}{n-1}\sum_{i\neq 1}d(v_1, v_i) + \frac{1}{n-1}\sum_{i\neq 2}d(v_2, v_i) + \cdots + \frac{1}{n-1}\sum_{i\neq n}d(v_n, v_i)\right)
$$
$$
=
$$
$$
\frac{1}{n(n-1)}\sum_{i\neq j}d(v_i, v_j).
$$
Best Answer
In you graph the shortest path between node $i$ and node $i$ is 0 (because it is the same node), and these paths have 0 hops (number of nodes of a path, excluding the first node, or number of edges of the path, i.e length of the path).
You have 3 paths of minimum length 1 ($0 \to 1$, $1 \to 2$, $3 \to 4$), and $1$ shortest path of length 2 (the one between nodes $0$ and $2$).
About you edit 2:
If you add a self-loop on the node $0$, you will have several paths from $0$ to $0$, including the trivial path of length $0$ and the path taking the self-loop, on length $1$. However this path of length $1$ is not a shortest path and therefore is not counted.