Re: Finding and changing strings in a matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg106775] Re: [mg106745] Finding and changing strings in a matrix
• From: Tomas Garza <tgarza10 at msn.com>
• Date: Sat, 23 Jan 2010 07:32:41 -0500 (EST)
• References: <201001221040.FAA02916@smc.vnet.net>

```I wonder if I understand your problem correctly: when you say "consecutive
text strings", you mean "consecutive" along the column? If such be the case, then

In[3]:= 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,"-","-"}};

In[4]:= trMyData=Transpose[myData];

In[5]:= fd=Table[Flatten[Position[trMyData[[j]],x_/;StringQ[x]]],{j,1,Length[trMyData]}]

Out[5]= {{7,8},{5,11},{6,7,11}}

In[6]:= Table[trMyData[[i]][[fd[[i,j]]]]=trMyData[[i]][[fd[[i,j]]-1]],{i,1,Length[trMyData]},{j,1,Length[fd[[i]]]}];

In[7]:= newMyData=Transpose[trMyData];

In[8]:= newMyData//MatrixForm

Out[8]

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

Tomas

> Date: Fri, 22 Jan 2010 05:40:02 -0500
> From: jagra24891 at mypacks.net
> Subject: [mg106745] Finding and changing strings in a matrix
> To: mathgroup at smc.vnet.net
>
> 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
>

--_bc380377-eef2-4e1a-8140-d101eb31f159_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html>
<style><!--
.hmmessage P
{
margin:0px;
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
<body class='hmmessage'>
I wonder if I understand your problem correctly: when you say "consecutive =
text strings", you mean "consecutive" along the column? If such be the ca=
se, then<br><br>In[3]:= 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.5=
5},{5397.86,10546.4,5957.43},{5412.88,"-","-"}};<br><br>In[4]=
:= trMyData=Transpose[myData];<br><br>In[5]:= fd=Table[Flatten[Po=
sition[trMyData[[j]],x_/;StringQ[x]]],{j,1,Length[trMyData]}]<br>=
<br>Out[5]= {{7,8},{5,11},{6,7,11}}<br><br>In[6]:= Table[tr=
MyData[[i]][[fd[[i,j]]]]=trMyData[[i]][[fd[[i,j]]-1]],{i,1,Leng=
th[trMyData]},{j,1,Length[fd[[i]]]}];<br><br>In[7]:= newMyData==
Transpose[trMyData];<br><br>In[8]:= newMyData//MatrixForm<br><br>Out[8]=
<br><br>5217.61&nbsp;&nbsp;&nbsp; 10163.8&nbsp;&nbsp;&nbsp; 584=
4.44<br>5196.81&nbsp;&nbsp;&nbsp; 10142.1&nbsp;&nbsp;&nbsp; 583=
1.21<br>5293.99&nbsp;&nbsp;&nbsp; 10183.5&nbsp;&nbsp;&nbsp; 593=
0.53<br>5328.66&nbsp;&nbsp;&nbsp; 10378.&nbsp;&nbsp;&nbsp; 5945=
.69<br>5372.38&nbsp;&nbsp;&nbsp; 10378.&nbsp;&nbsp;&nbsp; 5957.=
44<br>5402.41&nbsp;&nbsp;&nbsp; 10536.9&nbsp;&nbsp;&nbsp; 5957.=
44<br>5402.41&nbsp;&nbsp;&nbsp; 10494.7&nbsp;&nbsp;&nbsp; 5957.=
44<br>5402.41&nbsp;&nbsp;&nbsp; 10634.2&nbsp;&nbsp;&nbsp; 6002.=
92<br>5437.61&nbsp;&nbsp;&nbsp; 10638.1&nbsp;&nbsp;&nbsp; 6011.=
55<br>5397.86&nbsp;&nbsp;&nbsp; 10546.4&nbsp;&nbsp;&nbsp; 5957.=
43<br>5412.88&nbsp;&nbsp;&nbsp; 10546.4&nbsp;&nbsp;&nbsp; 5957.=
43<br><br>Tomas<br><br><br>&gt; Date: Fri, 22 Jan 2010 05:40:02 -0500<b=
r>&gt; From: jagra24891 at mypacks.net<br>&gt; Subject: [mg106745] Finding=
and changing strings in a matrix<br>&gt; To: mathgroup at smc.vnet.net<br>&=
gt; <br>&gt; Hi everyone!<br>&gt; <br>&gt; I have a matrix with {50=
0, 20} dimensions.  Some random positions in<br>&gt; each column have t=
ext strings in them instead of numbers.  While it<br>&gt; can happen, t=
he text strings do not typically appear across an entire<br>&gt; row of d=
ata.<br>&gt; <br>&gt; I need to find the text strings and replace them =
by the values<br>&gt; immediately above them.  In the case where the matr=
ix has consecutive<br>&gt; text strings I need to replace them with the f=
irst number above the<br>&gt; run of strings.<br>&gt; <br>&gt; Here's=
a part of the data file so you get the idea of what I start<br>&gt; with=
:<br>&gt; <br>&gt; myData ={{5217.61, 10163.8, 5844.44}, {5196.=
81, 10142.1, 5831.21},<br>&gt; {5293.99,<br>&gt;   10183.5, 5=
930.53}, {5328.66, 10378., 5945.69}, {5372.38, "-",<br>&gt;  =
5957.44}, {5402.41, 10536.9, "-"}, {"-", 10494.7, "-"}, {"-"=
,<br>&gt;   10634.2, 6002.92}, {5437.61, 10638.1, 6011.55}, {=
5397.86, 10546.4,<br>&gt;   5957.43}, {5412.88, "-", "-"}};<b=
r>&gt; <br>&gt; myData // MatrixForm<br>&gt; <br>&gt; I want to get=
rid of all "-"s and replace them with the numbers most<br>&gt; directly =
above them.<br>&gt; <br>&gt; I guess I could do this with loops, but =
that seems like a waste of<br>&gt; Mathematica's power.<br>&gt; <br>&gt=
; I've tried using Rest[myData] and Most[myData] then using Position to<b=
r>&gt; identify where one of the matrices had strings and the other didn'=
t<br>&gt; then using that to replace the strings in the original data.  B=
ut I<br>&gt; run into a snag when I have consecutive strings.<br>&gt; <=
br>&gt; I hope someone can help.  Cool forum, lots to learn!  Thanks.<b=
r>&gt; <br>&gt; Jagra<br>&gt; <br> 		 	   		  </body>
</html>=

--_bc380377-eef2-4e1a-8140-d101eb31f159_--

```

• Prev by Date: Re: More /.{I->-1} craziness. Schools are conservative. So are [people]
• Next by Date: Re: Re: NotebookGet/Read/EvaluateSelection Issues
• Previous by thread: Finding and changing strings in a matrix
• Next by thread: Re: Finding and changing strings in a matrix