Separating real and imaginary components of rectangular coordinates - matlab - string

I have a rectangular Coordinate in Matlab that looks like the following:
0.0240 - 0.1680i
I'd like to split the double into it real and imaginary parts, those parts being 0.0240 and -0.1680 (Don't need the i here)
I've converted the double into a string using the following:
I=0.0240 - 0.1680*i
I_1=num2str(I)
Im not sure how to proceed here to get what i want. strsplit() just gives back the string in the form it already is. Id like to somehow split it to give me the two numbers separately. I'm not too experienced with data manipulation in Matlab so any help is appreciated.

num2str converts number to string. It is not for separating real and imaginary parts.
You can use:
I=0.0240 - 0.1680*i;
real_part=real (I)
imaginary_part=imag(I)

Related

How to check and extract only numbers from a string with python

I have a dynamically generated string like:
'\n\n\n0\n1\n\n\n\n\n\n'
or
'\r\n\r\n\r\n0\r\n\r\n1\r\n\r\n'
or
'\r\n\r\n\r\n1/2\r\n\r\n1/2\r\n\r\n'
I wonder what is the best way to extract only the number 1, 0 or 1/2 with python 3
What I am doing now is use \r\n or \n to split the string and check each element in the list - I don't like my own way to process, there should be a better and elegant way to do that.
Thanks
Split on whitespace to retrieve words. Then turn each word into a number, a fraction. Then convert to floating point, in case you find that more convenient.
(No need to wrap it with list(), if you're happy with a generator.)
from fractions import Fraction
def get_nums(s):
"""Extracts fractional numbers from input string s."""
return list(map(float, (map(Fraction, s.split()))))
It all can be done as a single one-liner using list-comprehension:
numbers=[float(i) for i in myString.split()]
The split method of the string class will take care of the excess whitespace for you.

Convert string with commas into integer in Freemarker

I have a string with commas in between. How should I convert this string into an integer. I tried using
x?number
but that gives me the following error
Exceptionfreemarker.core.NonNumericalException
e.g. The string is "453,000". I need to convert this to 453000.
Is there any other way of doing this?
There's no function built in for parsing numbers with national formats. ?number only deals with computer format, because when numbers are transferred as strings (which should be already rare), that's what used to be used. So in principle x should be already a number when it gets to FreeMarker, or at least it should use computer format. If that's not possible, you will need a custom function (or method) for that.

Visual Basic 6 - Convert scientific notation to double

Very simple.
I want to be able to convert scientific notation to a double in Visual Basic 6.
For example :
5.97427068015303E-10
I have tried :
results.Text = CDbl("5.97427068015303E-10")
results.Text = CDbl(5.97427068015303E-10)
Not working...
The value 5.97427068015303E-10 already is a Double value. I don't think the word convert means what you think it does. Conversion functions certainly won't accomplish what you appear to be after.
I suspect what you are trying to express is that you want to format such tiny values as text in simple decimal notation. If so you might try something like:
Text1.Text = Format$(5.97427068015303E-10, "0.0########################")

String is wrong converted to float? (Blitzmax - Reflections)

I have a little Problem in Blitzmax.
I try to read an INI-file and if I read floats they are converted in a very strange way.
The line in the file which is concerned looks like that for example:
_fStrength=40.6
The Output of this looks like that:
DebugLog:_fStrength: 40.5999985
The code I use to read that works with reflection and looks like that:
For Local fld:TField = EachIn id.EnumFields()
fld.Set(obj, SearchInFile("TempWeapon" + index, fld.Name(), "Weapons.ini"))
DebugLog(fld.Name() + ": " + String(fld.Get(obj)))
Next
I found out, that this only happens if the number after the "." does not equal's 5 or 0.
I can't explain this behaviour, because if I do not use reflections, it works fine.
Could anyone help me please?
As you probably know, your computer stores numbers in binary code, using a limited size. 40.6 expanded in binary is a periodic sequence (101000.1001100110011001100..., infinitely), similarly to what happens when you try to write down the digits of 1/3) repeating and thus can not be represented exactly, so you get rounding errors.
The number of correct digits you get here looks like you are using single-precision floating point numbers, you can push the error further back by going to double, but it won't disappear.
As a reference, you might find Wikipedia on floating point helpful.

Quick variable question

I have an EditText object (et_travel) on my screen that's asking for miles traveled. I grab that data like this:
float travel = Float.parseFloat(et_travel.getText().toString());
if(travel > 40000){
I just discover that if someone puts 40000 in the EditText, everything works fine, but if they put 40,000 (adding a comma to the number), I force close on the float travel = ...statement.
How can I evaluate the number without having a problem from the user adding a comma?
Is this in Java? It appears to be, but I'm wondering if I'm mistaken. Regardless, I would suggest you remove all of the characters from the string that are not of a numeric type. A way to do this may be using a regular expression.
A way to do this in Java may be the following:
String input = et_travel.getText().toString();
input = input.replaceAll("[^0-9]", "");
float travel = Float.parseFloat(input);
...
This way, you strip anything that is a non-numeric value from the string first, and then attempt to do your work. Obviously do some error checking before this (like input is not null and such). One change that is needed however is that you may need to maintain the '.' character (if you're given non-integer values). This would require changing the first regex a bit.
Check here: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
What you need is some validation on the input. Before converting the string into a float parse the string. If there are any ','s then remove them. If there is just junk then reject the input, otherwise someone could put a word or anything else in the input and cause havoc in your program.
Check out
inputType to restrict user input
android:inputType="number"

Resources