From: Tomas Gallardo on
Hi:

I've got this x=1264x2 array in which I need to correct a value in the second column as function of the first column.

Say:
-0,028066464 -0,35
0,115032063 0,104781301
0,270528915 0,64417803
0,4322721 0,776582735
0,593809388 0,875641093

Thing is, I need to set a condition:
for i=1:1264
if x(i,1)<0.3;
....
Then, the value of x(i,2) must be replaced by the (fixed) next value larger than 0.3 in the first column. I want to have column 2 like this (doing nothing to the valus above the restriction):

-0,028066464 0,4322721
0,115032063 0,4322721
0,270528915 0,4322721
0,4322721 0,776582735
0,593809388 0,875641093

Any ideas ??? Thanks in advance !!!
From: Donn Shull on
"Tomas Gallardo" <galsato(a)gmail.com> wrote in message <hm3ahl$5fk$1(a)fred.mathworks.com>...
> Hi:
>
> I've got this x=1264x2 array in which I need to correct a value in the second column as function of the first column.
>
> Say:
> -0,028066464 -0,35
> 0,115032063 0,104781301
> 0,270528915 0,64417803
> 0,4322721 0,776582735
> 0,593809388 0,875641093
>
> Thing is, I need to set a condition:
> for i=1:1264
> if x(i,1)<0.3;
> ...
> Then, the value of x(i,2) must be replaced by the (fixed) next value larger than 0.3 in the first column. I want to have column 2 like this (doing nothing to the valus above the restriction):
>
> -0,028066464 0,4322721
> 0,115032063 0,4322721
> 0,270528915 0,4322721
> 0,4322721 0,776582735
> 0,593809388 0,875641093
>
> Any ideas ??? Thanks in advance !!!

To solve a problem like this you should look into logical indexing. Some functions which will help you are diff and find and the relational operators. Try help find, help diff, and help logical. Using the data in your example (reentered in US notation)

>> data

data =

-0.0281 -0.3500
0.1150 0.1048
0.2705 0.6442
0.4323 0.7766
0.5938 0.8756

>> data(:,1)

ans =

-0.0281
0.1150
0.2705
0.4323
0.5938

>> data(:,1) < .3

ans =

1
1
1
0
0

>> indices = find(data(:,1) < .3)

indices =

1
2
3

>> indices = find(data(:,1) < .3)

indices =

1
2
3

>> data(indices, 2) = data(indices(end)+1, 1)

data =

-0.0281 0.4323
0.1150 0.4323
0.2705 0.4323
0.4323 0.7766
0.5938 0.8756

since your problem has many groups of data needing replacement you will need to locate them which you can doo with diff ie

>> diff(data(:,1) < .3)

ans =

0
0
-1
0

This should help get you started.

Good Luck,

Donn