"Oluwa KuIse" <wespeakforex@yahoo.com> wrote in message <hs95d7$37i$1@fred.mathworks.com>...
> Hello,
> I'm solving problems in overland flow modeling and when fluid depth, h, goes below a set minimum, I want the velocity,U, to be set to zero.
> %%
> %Given matrices h and U
> min_h = 0.001; % arbitrarily set, any number can be put here
> [i,j] = find(h < min_h); % (i,j) is the position of elements of h that meet this criterion
> U(i,j) = 0; % put zero at the places where h was less than min_h i.e. the i,j.
> When I executed this program, all elements of U were replaced with 0 so I'm not getting something right. I will appreciate any help.
> Thanks,
> Michael
Use logical indexing, i.e.
>>U(h<min_h) = 0;
For your understanding;
The reason what you were doing didn't work is because in order to index with multiple (i,j) indices you need to use sub2ind, i.e.
>>U(sub2ind(size(U),i,j) = 0;
This takes the individual dimensional indices and converts them to linear indices which can be directly assigned directly.
This is slower and sloppier and can be avoided in this situation.
