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