From: Cris Luengo on
"Joshua" <joshua.edel(a)gmail.com> wrote in message <i1mno5$5ek$1(a)fred.mathworks.com>...
> thanks for all your comments. The complete code I have written is as follows. If you think this could be optimized further, do let me know.

Hi Joshua,
The code is a little mystifying, to say the least. :)
Some questions & comments:

> if rem(i-1,BinL) == 0
> p = p +1;
> end

You don't use this value of `p` later, is this intended?

> Fm_1 = MacroT(MacroT >= Mf(j) & MacroT < Mf(j+1))*1e-7;
> ch_1 =ch(MacroT >= Mf(j) & MacroT < Mf(j+1));

Put the result of the replicated code in a temporary variable:

I = MacroT >= Mf(j) & MacroT < Mf(j+1);
Fm_1 = MacroT(I)*1e-7;
ch_1 = ch(I)*1e-7;

> M_1 = Marker_1(Marker_1 >= Mf(j) & Marker_1 < Mf(j+1))*1e-7;
> M_2 = Marker_2(Marker_2 >= Mf(j) & Marker_2 < Mf(j+1))*1e-7;
>
> T_pix = (M_1(i+1) - M_2(i))/(Wd_bin);
> Fm_2 = Fm_1(Fm_1 >= M_2(i) & Fm_1 < M_1(i+1));
> ch_2 = ch_1(Fm_1 >= M_2(i) & Fm_1 < M_1(i+1));

Why are you indexing `M_1` and `M_2` using `i`, the variable of the outer loop? Are you trying to find the `i`th element of `Marker_1` within each interval? If you actually loop over `i`, maybe it would be better to reverse the order of the loops, that is, the outer loop being `j=1:10`, the inner loop being `i=6`. I don't know if you intend to loop over `i` or not, but since you have the `for` there...

Also, again, there's some replicated code when indexing into `Fm_1` and `ch_1`.

You are slicing `MacroT` and `ch` twice, once for values within `Mf(j)` and `Mf(j+1), then for values within `M_2(i)` and `M_1(i+1)`. It seems one of these steps is superfluous.

> Mat = sub2ind([MXch Wd_bin], ch_2, binRow);
> [Rep,I] = histc(Mat,min(Mat):max(Mat));
> Rep(Rep == 0) = [];
> p = unique(Mat);
> Decay(p) = Rep;

Are you sure you mean not to change the value of Decay at points outside `p`?
I would expect:
Decay = zeros(MXch,Wd_bin);
Decay(p) = Rep;
If so, this might be faster (code not tested!):
Mat = sub2ind([MXch Wd_bin], ch_2, binRow);
Decay(:) = histc(Mat,1:MXch*Wd_bin);

One more tip: add some comments to your code! you'll really appreciate it when you'll need to make changes next year and don't at all remember what all those variables mean and what the indexing is doing. :)

Cheers,
Cris.
From: Joshua on
thanks for comments - I wil make some of the suggested changes.

Joshua