Python: Manipulate an array - arrays

I have an array that looks just like this one:
array(['00:00;1;5950;6\r', '00:10;1;2115;6\r', '00:10;2;4130;6\r',
'00:10;3;5675;6\r', '00:20;1;1785;6\r'],
dtype='|S15')
For my evaluation I need only the value after the second semicolon at each array entry. Here in this example, I need the values:
5950, 2115, 4130, 5675, 1785.
Is it possible to manipulate the array in way to get the entries I want? And how can that be solved? I know how to remove the symbols, so in the end I get the array:
['0000159506\r', '0010121156\r', '0010241306\r', '0010356756\r', '0020117856\r']
But I don't know if this is the right way to handle these problem. Do anyone know what to do? Thank you very much!

Try this:
A = array(['00:00;1;5950;6\r', '00:10;1;2115;6\r', '00:10;2;4130;6\r',
'00:10;3;5675;6\r', '00:20;1;1785;6\r'],
dtype='|S15')
list_ = [str(x.split(";")[2]) for x in A]

The best way to get these values is to use the python split function.
You can choose the delimiter. So in this case the delimiter can be a ;.
result = line.split(';')
and then just find the third value of the resulting list.
num = result[2]

Related

Manipulating a 'dynamic' array in javascript

As part of my nightwatchjs testing script, I have a 'dynamic' array that I would like to add a character to.
So, at the moment my array has a value of 4000, but I would like this to read 4,000.
This means that I need to add a , to my array.
The issue I have however, is that this value could change the next time I run the test script, so it could be 10000 or 100000.
So I suppose what I'm asking is whether it's possible to "select a value 3 elements from the end of my array?"
So no matter what or how many elements are in the array, the array will read xx,000.
Any help would be much appreciated. Many thanks.
Does this array have a single value? If so, why is it an array instead of just a variable?
You can use toLocaleString() to add commas to a numeric value, but it will return a string and not a number.
let arr = [4000]
let num = arr[0]
let commas = num.toLocaleString()
console.log(commas)
// "4,000"

Use an array item as replacement in a MATLAB regexpr

I have a string containing a math function like this:
sin(x[1]) + cos(x[2]) + tan(x[3]) + x[1]
Now I want to replace each x[number] with a letter of the alphabet using regexpr. The result should look like this:
sin(a) + cos(b) + tan(c) + a
So I defined an alphabet array like this:
alphabet = ('a':'z')
This is my first regexpr that just replaces every x[number] with an 'a':
regexprep(functionString,'x\[(\d+)\]','${alphabet(1)}');
What I tried to make it replace with the right letter, is using $1 instead of 1. I thought this would not use alphabet(1) but dynamically the item at the right alphabet index.
regexprep(functionString,'x\[(\d+)\]','${alphabet($1)}');
Instead I am getting an error that the index exceeds the matrix dimensions.
Does anybody know what I am doing wrong? How do I get the right letter?
Thanks in advance!
Matlab uses the $1 input as text. Since int32('1') = 49 you result with an error Index exceeds matrix dimensions.
To solve your issue, use str2num:
regexprep(functionString,'x\[(\d+)\]','${alphabet(str2num($1))}')
In case you're interested, you can actually do this without having to create an alphabet variable. Here's how:
regexprep(functionString,'x\[(\d+)\]','${char($1+48)}')
Adding 48 to your index $1 and converting it to a char will give you ASCII characters starting at 'a'.
Have you tried regexprep(functionString,'x\[(\d+)\]','${alphabet($0)}');?
From what I see here : https://de.mathworks.com/help/matlab/ref/regexprep.html regex matches are 0 based, so the first one should be $0.

Apply a string value to several positions of a cell array

I am trying to create a string array which will be fed with string values read from a text file this way:
labels = textread(file_name, '%s');
Basically, for each string in each line of the text file file_name I want to put this string in 10 positions of a final string array, which will be later saved in another text file.
What I do in my code is, for each string in file_name I put this string in 10 positions of a temporary cell array and then concatenate this array with a final array this way:
final_vector='';
for i=1:size(labels)
temp_vector=cell(1,10);
temp_vector{1:10}=labels{i};
final_vector=horzcat(final_vector,temp_vector);
end
But when I run the code the following error appears:
The right hand side of this assignment has too few values to satisfy the left hand side.
Error in my_example_code (line 16)
temp_vector{1:10}=labels{i};
I am too rookie in cell strings in matlab and I don't really know what is happening. Do you know what is happening or even have a better solution to my problem?
Use deal and put the left hand side in square brackets:
labels{1} = 'Hello World!'
temp_vector = cell(10,1)
[temp_vector{1:10}] = deal(labels{1});
This works because deal can distribute one value to multiple outputs [a,b,c,...]. temp_vector{1:10} alone creates a comma-separated list and putting them into [] creates the output array [temp_vector{1}, temp_vector{2}, ...] which can then be populated by deal.
It is happening because you want to distribute one value to 10 cells - but Matlab is expecting that you like to assign 10 values to 10 cells. So an alternative approach, maybe more logic, but slower, would be:
n = 10;
temp_vector(1:n) = repmat(labels(1),n,1);
I also found another solution
final_vector='';
for i=1:size(labels)
temp_vector=cell(1,10);
temp_vector(:,1:10)=cellstr(labels{i});
final_vector=horzcat(final_vector,temp_vector);
end

Objects in Arrays Help Lua?

So, I have an array
//loop here
nummobs = nummobs + 1
Mobs = {}
Mobs[nummobs] = Entity.Init(x(locations to spawn mob), y(locations to spawn mob),"testMob")
Then, call the draw method...
for i = 0, table.getn(Mobs) do
Mobs[i].draw()
end
Error: map.lua:54(Mobs[i].draw() line): attempt to index field '?' (a nil value)... BUT IT HAS SOMETHING IN IT! right?
Anyone ever try something like this? Can anyone fix it?
Thanks
Nate
Lua uses 1-based indexes for arrays. Thus, the range of an array is [1, n] inclusive, where n is the number of elements.
More importantly, you can use ipairs and not have to write out the loop components:
for i, mob in ipairs(Mobs) do
mob:draw()
end
Oh, and never use getn; use the # length operator instead.

matlab Is there something like list comprehension as it is in python?

I am looking for something like list comprehensions in matlab however I couldnt find anything like this in the documentary.
In python it would be something like
A=[i/50 for i in range(50)]
Matlab is very fond of 'vectorizing'. You would write your example as:
A = (0:49) ./ 50
Matlab hates loops and therefore list comprehension. That said, take a look at the arrayfun function.
You can do:
(1:50)/50
Or for something more general, you can do:
f=#(x) (x/50);
arrayfun(f,1:50)
No, Matlab does not have list comprehensions. You really don't need it, as the focus should be on array-level computations:
A = (1:50) / 50
Matlab can work with arrays directly, making list comprehension less useful
If what you're trying to do is as trivial as the sample, you could simply do a scalar divide:
A = (0:50) ./ 50
There are several ways to generate a list in Matlab that goes from 0 to 49/50 in increments of 1/50
A = (0:49)/50
B = 0:1/50:49/50
C = linspace(0,49/50,50)
EDIT As Sam Roberts pointed out in the comments, even though all of these lists should be equivalent, the numerical results are different due to floating-point errors. For example:
max(abs(A-B))
ans =
1.1102e-16
This doesn't work help with your numerical example but for the special case of strings there is the compose function that does the same thing as a list comprehension of the form:
s = [f"Label_{i}" for i in range(1, 6)]
Example:
str = compose("Label_%d", 1:5)
Result:
str =
1×5 string array
"Label_1" "Label_2" "Label_3" "Label_4" "Label_5"

Resources