[Date Index]
[Thread Index]
[Author Index]
Re: Finding and changing strings in a matrix
*To*: mathgroup at smc.vnet.net
*Subject*: [mg106802] Re: Finding and changing strings in a matrix
*From*: Ray Koopman <koopman at sfu.ca>
*Date*: Sat, 23 Jan 2010 07:38:03 -0500 (EST)
*References*: <hjbv9l$2qj$1@smc.vnet.net>
On Jan 22, 2:39 am, Jagra <jagra24... at mypacks.net> wrote:
> Hi everyone!
>
> I have a matrix with {500, 20} dimensions. Some random positions in
> each column have text strings in them instead of numbers. While it
> can happen, the text strings do not typically appear across an entire
> row of data.
>
> I need to find the text strings and replace them by the values
> immediately above them. In the case where the matrix has consecutive
> text strings I need to replace them with the first number above the
> run of strings.
>
> Here's a part of the data file so you get the idea of what I start
> with:
>
> myData ={{5217.61, 10163.8, 5844.44}, {5196.81, 10142.1, 5831.21},
> {5293.99,
> 10183.5, 5930.53}, {5328.66, 10378., 5945.69}, {5372.38, "-",
> 5957.44}, {5402.41, 10536.9, "-"}, {"-", 10494.7, "-"}, {"-",
> 10634.2, 6002.92}, {5437.61, 10638.1, 6011.55}, {5397.86, 10546.4,
> 5957.43}, {5412.88, "-", "-"}};
>
> myData // MatrixForm
>
> I want to get rid of all "-"s and replace them with the numbers most
> directly above them.
>
> I guess I could do this with loops, but that seems like a waste of
> Mathematica's power.
>
> I've tried using Rest[myData] and Most[myData] then using Position to
> identify where one of the matrices had strings and the other didn't
> then using that to replace the strings in the original data. But I
> run into a snag when I have consecutive strings.
>
> I hope someone can help. Cool forum, lots to learn! Thanks.
>
> Jagra
This is not very efficient, but what it does should be clear.
myData = {
{5217.61, 10163.8, 5844.44},
{5196.81, 10142.1, 5831.21},
{5293.99, 10183.5, 5930.53},
{5328.66, 10378., 5945.69},
{5372.38, "-", 5957.44},
{5402.41, 10536.9, "-"},
{"-", 10494.7, "-"},
{"-", 10634.2, 6002.92},
{5437.61, 10638.1, 6011.55},
{5397.86, 10546.4, 5957.43},
{5412.88, "-", "-"}};
Scan[({i,j} = #; x[[i,j]] = x[[i-1,j]])&,
Position[x = myData, _String]];
ColumnForm[x]
{5217.61, 10163.8, 5844.44}
{5196.81, 10142.1, 5831.21}
{5293.99, 10183.5, 5930.53}
{5328.66, 10378., 5945.69}
{5372.38, 10378., 5957.44}
{5402.41, 10536.9, 5957.44}
{5402.41, 10494.7, 5957.44}
{5402.41, 10634.2, 6002.92}
{5437.61, 10638.1, 6011.55}
{5397.86, 10546.4, 5957.43}
{5412.88, 10546.4, 5957.43}
Prev by Date:
**Re: Finding and changing strings in a matrix**
Next by Date:
**Re: Re: More /.{I->-1} craziness**
Previous by thread:
**Re: Finding and changing strings in a matrix**
Next by thread:
**Re: Finding and changing strings in a matrix**
| |