Complex Number Conversion to character form

Notable Behavior of Conversions with Formatting Operators:
  1. Numeric conversions print only the real component of complex numbers.
  2. If you specify a conversion that does not fit the data, such as a character conversion for a numeric value, MATLAB® overrides the specified conversion, and uses %e.
Is there any way by which I can print real part as well as imaginary part of a complex number in string form?
Can I fit a large scale data in character conversion?
I am implementing an algorithm to encrypt text using complex number and after encrypting just want to convert that encrypted data (complex number) into character form. Because of number format limitation, I am unable to convert it. Please help me to solve this problem...

1 件のコメント

Adam
Adam 2016 年 11 月 11 日
編集済み: Adam 2016 年 11 月 11 日
I don't see why this can't be achieved by simply using sprintf, but you haven't given any examples so maybe I am just misunderstanding what you are looking for.

サインインしてコメントする。

回答 (2 件)

Guillaume
Guillaume 2016 年 11 月 11 日

0 投票

What is large scale data?
I don't see the problem in printing / converting complex numbers:
n = pi + 1i*log(2);
s = sprintf('%g + %gi', real(n), imag(n)) %or whichever format you want
And if n is an array:
s = arrayfun(@(x) sprintf('%g + %gi', real(x), imag(x)), n, 'UniformOutput', false)

2 件のコメント

Shafali
Shafali 2016 年 11 月 13 日
This function does not help me to produce the output in required character form.
Shafali
Shafali 2016 年 11 月 13 日
As I said that I am trying to implement an encryption algorithm so in this I am doing arithmetic operation with the help of 128bit (digits) complex number key. That result I want to show in character form.

サインインしてコメントする。

Steven Lord
Steven Lord 2016 年 11 月 11 日

0 投票

Based on your previous questions, you're going to try to convert a symbolic complex number into a char array. Is that correct? If so use the char function on your symbolic expression, possibly in conjunction with the vpa function.

4 件のコメント

Shafali
Shafali 2016 年 11 月 13 日
編集済み: Walter Roberson 2016 年 11 月 13 日
by using char(u), I am getting output in the given form:
matrix([[104.000397380156931421502543519603587306237636317610462580017 + 0.001...
whereas I want output in abcdef... form (character form).
Walter Roberson
Walter Roberson 2016 年 11 月 13 日
arrayfun(@char, YourSymbolicMatrix, 'uniform', 0)
Shafali
Shafali 2016 年 11 月 14 日
again getting output in single quote form like '104.000397...' by applying the above function.
Walter Roberson
Walter Roberson 2016 年 11 月 14 日
Yes. You are asking to process an array of values, not a single value, and you have not said anything about how you want to code the array markers. For example do you want the items written out using '[' and ']' and ';' between rows and ',' between items? What do you want done if the matrix has more than 2 dimensions?
I would caution you that if you are wanting the quotes removed then chances are that you are trying to create you intend to execute, which would be a mistake because without the quotes the value is going to be converted as a plain double precision number.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2016 年 11 月 11 日

コメント済み:

2016 年 11 月 14 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by