Filtering a Cell Array with Recursion - arrays

I'm pretty close on this problem. What I have to do is filter out a cell array. The cell array can have a variety of items in it, but what I want to do is pull out the strings, using recursion. I am pretty close on this one. I just have an issue when the cells have spaces in them. This is what I should get:
Test Cases:
cA1 = {'This' {{{[1:5] true} {' '}} {'is '} false true} 'an example.'};
[filtered1] = stringFilter(cA1)
filtered1 => 'This is an example.'
cA2 = {{{{'I told '} 5:25 'her she'} {} [] [] ' knows'} '/take aim and reload'};
[filtered2] = stringFilter(cA2)
filtered2 => 'I told her she knows/take aim and reload'
Here is what I have:
%find the strings in the cArr and then concatenate them.
function [Str] = stringFilter(in)
Str = [];
for i = 1:length(in)
%The base case is a single cell
if length(in) == 1
Str = ischar(in{:,:});
%if the length>1 than go through each cell and find the strings.
else
str = stringFilter(in(1:end-1));
if ischar(in{i})
Str = [Str in{i}];
elseif iscell(in{i})
str1 = stringFilter(in{i}(1:end-1));
Str = [Str str1];
end
end
end
end
I tried to use 'ismember', but that didn't work. Any suggestions? My code outputs the following:
filtered1 => 'This an example.'
filtered2 => '/take aim and reload'

You can quite simplify your function to
function [Str] = stringFilter(in)
Str = [];
for i = 1:length(in)
if ischar(in{i})
Str = [Str in{i}];
elseif iscell(in{i})
str1 = stringFilter(in{i});
Str = [Str str1];
end
end
end
Just loop through all elements in the cell a test, whether it is a string or a cell. In the latter, call the function for this cell again. Output:
>> [filtered1] = stringFilter(cA1)
filtered1 =
This is an example.
>> [filtered2] = stringFilter(cA2)
filtered2 =
I told her she knows/take aim and reload

Here is a different implememntation
function str = stringFilter(in)
if ischar(in)
str = in;
elseif iscell(in) && ~isempty(in)
str = cell2mat(cellfun(#stringFilter, in(:)', 'uni', 0));
else
str = '';
end
end
If it's string, return it. If it is a cell apply the same function on all of the elements and concatenate them. Here I use in(:)' to make sure it is a row vector and then cell2mat concatenates resulting strings. And if the type is anything else return an empty string. We need to check if the cell array is empty or not because cell2mat({}) is of type double.

The line
Str = ischar(in{:,:});
is the problem. It doesn't make any sense to me.
You're close to the getting the answer, but made a few significant but small mistakes.
You need to check for these things:
1. Loop over the cells of the input.
2. For each cell, see if it itself is a cell, if so, call stringFilter on the cell's VALUE
3. if it is not a cell but is a character array, then use its VALUE as it is.
4. Otherwise if the cell VALUE contains a non character, the contribution of that cell to the output is '' (blank)
I think you made a mistake by not taking advantage of the difference between in(1) and in{1}.
Anyway, here's my version of the function. It works.
function [out] = stringFilter(in)
out = [];
for idx = 1:numel(in)
if iscell (in{idx})
% Contents of the cell is another cell array
tempOut = stringFilter(in{idx});
elseif ischar(in{idx})
% Contents are characters
tempOut = in{idx};
else
% Contents are not characters
tempOut = '';
end
% Concatenate the current output to the overall output
out = [out, tempOut];
end
end

Related

VBScript function to read multiple strings not working properly

I have a function to read multiple string that I copied from another thread here.
I have the whole document that I need in a string name strResult, my intention is to compare only the fields on the strList, and find them in the strResult.
This is the function:
Function FindString(strCheck,strFind)
Arr = Split(strResult,",")
Flag = 0
And I call it like this: Call FindString(strResult,strList)
For Each str in Arr
If InStr(strCheck, str) > 0 Then
Flag = 1
Reporter.ReportEvent micPass,"Field Found","Field:"&str&" was found"
Else
Flag = 0
Reporter.ReportEvent micFail,"Field not Found","Field:"&str&" was not found"
End If
Next
If Flag = 1 Then
FindString = True
Reporter.ReportEvent micPass,"Field Found","Field"&str&"was found"
Else
FindString = False
Reporter.ReportEvent micFail,"Field not found","Field"&str&"was not found"
End If
It should return fail when the fields are not found, but it just ignores them, The list of string is on a variable that contains something like "field1,"&_"field2", the main problem is that even if "field3" is not in the strList, it will display it as found, and I only want it to take the fields that are on the strList not all of the strResult string
I fixed the function by splitting the list of values too, like this
Arr = Split(strCheck,",")
Arr2 = Split(strFind,",")
Flag = 0
For Each str in Arr
For Each str2 in Arr2
If InStr(str, str2) > 0 Then
Flag = 1
Reporter.ReportEvent micPass,"Field "&str2&" Found","Field:"&str&" was found"
Exit For
End If
Next

Run parallel loops in Ruby

I have two sets of arrays stored in a file and I need to extract values one by one and compare them. I am using this code but does look like I am doing correctly.
# First Dataset
File.foreach(file_set_a) do |data_a|
data_array_a = data_a.split("\t")
#file_name_a = data_array_a[0]
#file_ext_a = data_array_a[1]
# Second Dataset
File.foreach(file_set_b) do |data_b|
data_array_b = data_b.split("\t")
#file_name_b = data_array_b[0]
#file_ext_b = data_array_b[1]
#Compare
#file_name_a == #file_name_b
end
end
The problem is, I cannot go back and extract the next values in the set A when I enter the set B. Any suggestions?
First, convert those 2 files into two separated data arrays
lines_array_a = File.readlines(file_set_a)
lines_array_b = File.readlines(file_set_b)
I am assuming both of the array size will be same. Now run a loop and get the items from both array to compare them.
for i in 0..(lines_array_a.count - 1) do
data_array_a = lines_array_a[i].split("\t")
#file_name_a = data_array_a[0]
#file_ext_a = data_array_a[1]
data_array_b = lines_array_b[i].split("\t")
#file_name_b = data_array_b[0]
#file_ext_b = data_array_b[1]
#file_name_a == #file_name_b
end

Matlab not solving if statements

I'm having a bit of trouble iterating through a few if statements more than eight times. The code seems to work fine for the first several comparisons, performs the arithmetic and return/saves the output row 'export_data'. However, after that, it only returns the else condition and response. The variables beings assessed have 1500 rows each. I've added the code below and two photos showing the outputs. Any insight will be very much appreciated.
function [export_data] = WS_Zones(Forecast_WS, Observed_WS)
if (Forecast_WS > Observed_WS)
WS_Zone_1 = Observed_WS.*1.24;
WS_Zone_2 = Observed_WS.*1.28;
elseif (Forecast_WS < Observed_WS)
WS_Zone_1 = Observed_WS.*0.76;
WS_Zone_2 = Observed_WS.*0.72;
else
WS_Zone_1 = Observed_WS;
WS_Zone_2 = Observed_WS;
end
export_data=[Forecast_WS Observed_WS WS_Zone_1 WS_Zone_2];
filename = 'testdata.xlsx';
sheet = 1;
xlRange = 'A1';
xlswrite(filename,export_data,sheet,xlRange)
end
Expected Output
Wrong Output
This statement:
if [1 2 3] > [1 1 1]
disp('hello');
end
will never print "hello" even though 2 and 3 are both greater than 1. This is because the if statement needs to evaluate to either scalar true or false. If a vector is used, than only the first element is used to determine if the statement is true or not (comparisons between other elements are ignored). You can use any and all if you want to apply conditions on all elements.
If Forecast_WS and Observed_WS aren't scalars then you need to wrap your if statement in a for loop, e.g.:
WS_Zone_1 = Observed_WS;
WS_Zone_2 = Observed_WS;
for i = 1:numel(Forecast_WS)
if Forecast_WS(i) > Observed_WS(i)
WS_Zone_1(i) = Observed_WS(i).*1.24;
WS_Zone_2(i) = Observed_WS(i).*1.28;
elseif Forecast_WS(i) < Observed_WS(i)
WS_Zone_1(i) = Observed_WS(i).*0.76;
WS_Zone_2(i) = Observed_WS(i).*0.72;
end
end
or vectorize it using logical indexing:
WS_Zone_1 = Observed_WS;
WS_Zone_2 = Observed_WS;
idx = (Forecast_WS > Observed_WS);
WS_Zone_1(idx) = Observed_WS(idx).*1.24;
WS_Zone_2(idx) = Observed_WS(idx).*1.28;
idx = (Forecast_WS < Observed_WS);
WS_Zone_1(idx) = Observed_WS(idx).*0.76;
WS_Zone_2(idx) = Observed_WS(idx).*0.72;

Lua/LOVE indexing problems

I'm getting a very irritating error whenever I do anything like this with arrays. I have code that sets up the array in the love.load() function:
function iceToolsInit()
objectArray = {} --for object handling
objectArrayLocation = 0
end
and then code that allows for the creation of an object. It basically grabs all of the info about said object and plugs it into an array.
function createObject(x, y, renderimage) --used in the load function
--objectArray is set up in the init function
objectArrayLocation = objectArrayLocation + 1
objectArray[objectArrayLocation] = {}
objectArray[objectArrayLocation]["X"] = x
objectArray[objectArrayLocation]["Y"] = y
objectArray[objectArrayLocation]["renderimage"] =
love.graphics.newImage(renderimage)
end
After this, an update function reads through the objectArray and renders the images accordingly:
function refreshObjects() --made for the update function
arrayLength = #objectArray
arraySearch = 0
while arraySearch <= arrayLength do
arraySearch = arraySearch + 1
renderX = objectArray[arraySearch]["X"]
renderY = objectArray[arraySearch]["Y"]
renderimage = objectArray[arraySearch]["renderimage"]
if movingLeft == true then --rotation for rightfacing images
renderRotation = 120
else
renderRotation = 0
end
love.graphics.draw(renderimage, renderX, renderY, renderRotation)
end
end
I of course clipped some unneeded code (just extra parameters in the array such as width and height) but you get the gist. When I set up this code to make one object and render it, I get this error:
attempt to index '?' (a nil value)
the line it points to is this line:
renderX = objectArray[arraySearch]["X"]
Does anyone know what's wrong here, and how I could prevent it in the future? I really need help with this.
It's off-by-one error:
arraySearch = 0
while arraySearch <= arrayLength do
arraySearch = arraySearch + 1
You run through the loop arrayLength+1 number of times, going through indexes 1..arrayLength+1. You want to go through the loop only arrayLength number of times with indexes 1..arrayLength. The solution is to change the condition to arraySearch < arrayLength.
Another (more Lua-ly way) is to write this as:
for arraySearch = 1, #objectArray do
Even more Lua-ly way is to use ipairs and table.field reference instead of (table["field"]):
function refreshObjects()
for _, el in ipairs(objectArray) do
love.graphics.draw(el.renderimage, el.X, el.Y, movingLeft and 120 or 0)
end
end
objectArray and movingLeft should probably be passed as parameters...

Process output of a sequence of files in Matlab

EDIT 3
Hi! I had problems with the matrix dimensions but I've solved it. Now my problem is that I want to do the same operation on a large series of files on the same folder and I want write the output values on a separate line on text.txt. With the first one it works but it doesn't 'write' to the 'text', the rest. Is there something wrong?
myPath = 'C:\EX\';
a= dir (fullfile(myPath,'*.DIM'));
fileNames = { a.name };
% Rename files
for k = 1:length(fileNames)
newFileName = [fileNames{k}(1:2) fileNames{k}(4:6) '.txt'];
movefile([myPath fileNames{k}], [myPath newFileName]);
end
filePattern=fullfile( myPath,'*.txt');
txtFiles= dir(filePattern);
for k = 1:length(txtFiles)
baseFileName=txtFiles(k).name;
fullFileName= fullfile(myPath,baseFileName);
fid=fopen(fullFileName, 'r');
for i = 1:18
m{i} = fgetl(fid);
end
result2 = m{18};
result2b= result2([12:19]);
fid=fopen(fullFileName, 'r');
for i = 1:30
m{i} = fgetl(fid);
end
result3 = m{30};
result3b= result3([12:19]);
fid=fopen(fullFileName, 'r');
for i = 1:31
m{i} = fgetl(fid);
end
result4 = m{31};
result4b= result4([12:20]);
fid=fopen(fullFileName, 'r');
for i = 1:19
m{i} = fgetl(fid);
end
result5 = m{19};
result5b= result5([12:20]);
text= {baseFileName, result2b, result3b, result4b, result5b};
final= [Fields'; text];
end
Really thanks in advance!
Index exceeds dimensions is exactly what it means.
Try to put a breakpoint at the line where it occurs and check the dimension of result2. Assuming it is a vector, you will find that its length is less than 19.

Resources