Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
replacing elements of a matrix

Subject: replacing elements of a matrix

From: Oluwa KuIse

Date: 10 May, 2010 14:32:07

Message: 1 of 5

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

Subject: replacing elements of a matrix

From: dpb

Date: 10 May, 2010 14:38:56

Message: 2 of 5

Oluwa KuIse wrote:
> 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
...

> U(i,j) = 0; % put zero at the places where h was less than min_h i.e.
...


U(U<min_h) = 0;

--

Subject: replacing elements of a matrix

From: Sean

Date: 10 May, 2010 14:44:06

Message: 3 of 5

"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.

Subject: replacing elements of a matrix

From: Oluwa KuIse

Date: 10 May, 2010 15:55:23

Message: 4 of 5

Thanks for your answers, it worked. I have a related problem still in overland flow: For certain values of fluid depth e.g. h < 0.001, I want to set the ground slope, Sox, equal to the friction slope, Sfx.
Pseudocode:
%given h, Sox and Sfx all of the same size
For (i,j) = h <0.001
Sfx(i,j) = Sox(i,j); % Replace the elements in positions (i,j) in Sfx with their
                             corresponding values in Sox.
end

Regards,
Michael

Subject: replacing elements of a matrix

From: Sean

Date: 10 May, 2010 16:08:24

Message: 5 of 5

"Oluwa KuIse" <wespeakforex@yahoo.com> wrote in message <hs9a9b$pnu$1@fred.mathworks.com>...
> Thanks for your answers, it worked. I have a related problem still in overland flow: For certain values of fluid depth e.g. h < 0.001, I want to set the ground slope, Sox, equal to the friction slope, Sfx.
> Pseudocode:
> %given h, Sox and Sfx all of the same size
> For (i,j) = h <0.001
> Sfx(i,j) = Sox(i,j); % Replace the elements in positions (i,j) in Sfx with their
> corresponding values in Sox.
> end

We've given you all of the tools you need to do this.

>>Sfx(h<hmin) = Sox(h<hmin);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us