matlab - Performing a function on each matrix value -
i experimenting matlab functions. trying perform function on each value found in matrix such next simple example:
k = [1:100]; p = [45 60 98 100; 46 65 98 20; 47 65 96 50]; p(find(p)) = getsum(k, find(p), find(p) + 1); function x = getsum(k, f, g, h) x = sum(k(f:g)); end
why corresponding output matrix values 3, in other words why indices depending on first calculated sum?
the output following:
p = 3 3 3 3 3 3 3 3 3 3 3 3
f:g
returns value between f(1,1)
, g(1,1)
, 1:2
.
find(p)
returns indices of non 0 values. since values non-zero, indices.
so if break downwards statement p(find(p)) = getsum(k, find(p), fin(p) + 1)
we
find(p) = 1:12
we f = 1:12
, g = 2:13
lead k = 1:2
(as explained above)
finally sum(1:2) = 3
and value apply on p(1:12)
, same p(:,:)
(all matrix)
matlab matrix
No comments:
Post a Comment