Message Boards Message Boards

0
|
8555 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

Making a filter

Posted 10 years ago

I found this filter in a MatLab program:

F2=H2./G2;

for i=1:31 if G2(i,1)==0 F2(i,1)=0;

    end

end

It says that if any element between 1 and 31 in G2 is 0 (or very small that it's practically 0) then any element in F2 should be 0 between 1 and 31. How can I write something similar in Mathematica?

Thank you in advance.

POSTED BY: sean roubion

I am not completely sure that I understand the question. The way you phrase it seems different than what the Matlab code seems to be saying. Your statement seems to say that if there's a single zero in the g2 list between the 1st and 31st item then all the items in the f2 list should be 0 between the 1st and 31st item. The Matlab code seems to be saying that the corresponding items in the f2 list to the zero entries in the g2 list should be made zero. Which would you like to do?

Here is an approach to the first interpretation (which seems not to be what the MatLab code is doing:

In[1]:= g2 = RandomReal[{.1, 1}, 40];

In[2]:= f2 = RandomReal[{.1, 1}, 40];

In[3]:= If[ MatchQ[Min[g2[[;; 31]]], 0 | 0.0], f2[[;; 31]] = 0] 

In[4]:= f2

Out[4]= {0.698859, 0.679363, 0.423911, 0.483623, 0.787595, 0.612751, \
0.40243, 0.843678, 0.311282, 0.845609, 0.299857, 0.538917, 0.589583, \
0.483163, 0.19414, 0.578614, 0.525849, 0.562604, 0.21003, 0.218967, \
0.124448, 0.424428, 0.121839, 0.785187, 0.892473, 0.754695, 0.641295, \
0.129707, 0.311679, 0.297364, 0.712806, 0.452036, 0.98209, 0.729761, \
0.759641, 0.193543, 0.321016, 0.878235, 0.294693, 0.47783}

In[5]:= g2 = ReplacePart[g2, 5 -> 0]

Out[5]= {0.973667, 0.520102, 0.322591, 0.810693, 0, 0.290913, \
0.81454, 0.320626, 0.440353, 0.102121, 0.507082, 0.610275, 0.334749, \
0.572815, 0.764794, 0.952593, 0.598201, 0.240492, 0.760991, 0.685671, \
0.353344, 0.767221, 0.196774, 0.455549, 0.745013, 0.669929, 0.949023, \
0.135557, 0.345527, 0.57963, 0.931332, 0.107634, 0.652246, 0.604232, \
0.422684, 0.301714, 0.535314, 0.555032, 0.586025, 0.66681}

In[6]:= If[ MatchQ[Min[g2[[;; 31]]], 0 | 0.0], f2[[;; 31]] = 0] 

Out[6]= 0

In[7]:= f2

Out[7]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.452036, 0.98209, 0.729761, \
0.759641, 0.193543, 0.321016, 0.878235, 0.294693, 0.47783}

Here is one approach to the second interpretation:

In[2]:= g2 = RandomReal[{.1, 1}, 40];

In[4]:= g2 = ReplacePart[g2, {5 -> 0, 9 -> 0}]

Out[4]= {0.230691, 0.639585, 0.558889, 0.229718, 0, 0.827236, \
0.174744, 0.857889, 0, 0.455408, 0.86698, 0.380685, 0.809134, \
0.431981, 0.904197, 0.285284, 0.661542, 0.730948, 0.163582, 0.488968, \
0.737741, 0.145129, 0.477019, 0.541634, 0.432796, 0.618783, 0.417761, \
0.679858, 0.535588, 0.403186, 0.671502, 0.135117, 0.7191, 0.545737, \
0.156862, 0.64611, 0.643755, 0.273114, 0.335791, 0.148722}

In[5]:= f2 = RandomReal[{.1, 1}, 40];

In[6]:= zeroPositions = Flatten@Position[g2, 0 | 0.0]

Out[6]= {5, 9}

In[8]:= f2 = ReplacePart[f2, Thread[zeroPositions -> 0]]

Out[8]= {0.176758, 0.559801, 0.46467, 0.104863, 0, 0.429054, \
0.911004, 0.782487, 0, 0.880667, 0.867404, 0.964007, 0.241012, \
0.306318, 0.430069, 0.892654, 0.231851, 0.483184, 0.396506, 0.807405, \
0.451575, 0.948987, 0.161917, 0.349105, 0.800152, 0.123666, 0.966727, \
0.152303, 0.343645, 0.970657, 0.988564, 0.732112, 0.524944, 0.224724, \
0.186885, 0.842012, 0.853349, 0.563648, 0.999071, 0.16213}

And here is a very procedural approach that is made to look like what the Matlab thing is doing...

In[1]:= g2 = RandomReal[{.1, 1}, 40];

In[2]:= g2 = ReplacePart[g2, {5 -> 0, 9 -> 0}]

Out[2]= {0.619508, 0.822774, 0.225996, 0.375215, 0, 0.560112, \
0.156866, 0.516555, 0, 0.578736, 0.251348, 0.789907, 0.206223, \
0.369297, 0.233854, 0.72312, 0.666346, 0.774993, 0.869128, 0.26047, \
0.191136, 0.928748, 0.79583, 0.449014, 0.867915, 0.807733, 0.557077, \
0.362091, 0.967622, 0.224682, 0.123236, 0.231585, 0.247277, 0.212286, \
0.470056, 0.900049, 0.423985, 0.159262, 0.136103, 0.467753}

In[3]:= f2 = RandomReal[{.1, 1}, 40];

In[4]:= Do[If[MatchQ[g2[[i]], 0 | 0.0], f2[[i]] = 0], {i, 1, 31}];

In[5]:= f2

Out[5]= {0.231379, 0.305591, 0.574086, 0.465205, 0, 0.474641, \
0.516748, 0.522352, 0, 0.116524, 0.144332, 0.360361, 0.935265, \
0.166802, 0.116729, 0.730945, 0.13133, 0.652938, 0.55189, 0.653582, \
0.313277, 0.293528, 0.771225, 0.70616, 0.449017, 0.42196, 0.731467, \
0.272375, 0.881962, 0.19469, 0.85515, 0.762194, 0.132967, 0.448983, \
0.842266, 0.196552, 0.608901, 0.510816, 0.374962, 0.964426}
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract