フィルターのクリア

How to make double array representation as complex array with null imaginary number ?

2 ビュー (過去 30 日間)
EL MOUSLIH
EL MOUSLIH 2024 年 4 月 29 日
編集済み: Stephen23 2024 年 4 月 29 日
Hello,
I have a double cell array with only real parts, i want it to be a double complex cell array with zero imagineray part like this representation: for example : 3 + 0.00000000i for the whole array , what i want to be interested is the representation which contains real part and imaginary part which is zero,
Do you have any idea to make this possible in Matlab ?
  1 件のコメント
Stephen23
Stephen23 2024 年 4 月 29 日
編集済み: Stephen23 2024 年 4 月 29 日
"I have a double cell array with only real parts, i want it to be a double complex cell array"
What exactly is a "double cell array" ?
MATLAB has data types DOUBLE and CELL, but not a "double cell array" nor a "double complex cell array":

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

採用された回答

KSSV
KSSV 2024 年 4 月 29 日
z = 3+eps*1i
z = 3.0000 + 0.0000i
  2 件のコメント
EL MOUSLIH
EL MOUSLIH 2024 年 4 月 29 日
Thank you it works like sharm
data = [1;2;3;4;5;6;7;8;9;10]
for i=1:numel(data)
disp(data(i,1))
NullImg = eps*1
data(i,1) = complex(data(i),NullImg)
end
Stephen23
Stephen23 2024 年 4 月 29 日
Note that it fails your requirement "imaginary part which is zero".

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

その他の回答 (2 件)

Paul
Paul 2024 年 4 月 29 日
In general, use complex
A = rand(2);
C = complex(A,0)
C =
0.6545 + 0.0000i 0.0714 + 0.0000i 0.0427 + 0.0000i 0.1873 + 0.0000i
If you have multiple real arrays stored in cells of a cell array, you can convert each cell element with a loop.
Are you sure that adding a zero imaginary part to your array(s) is necessary?
  3 件のコメント
Stephen23
Stephen23 2024 年 4 月 29 日
編集済み: Stephen23 2024 年 4 月 29 日
"This solution won't give me a cell array with representaion as zero in the imaginary number"
The answer you accept also does not "give" you a cell array: none of these answers have anything to do with cell arrays. It is uncelar what you mean by "double complex cell array" because nothing you have shown, explained, commented, or accepted has anything to do with cell arrays.
In any case, this solution using COMPLEX() does return a complex number with imaginary part equal to zero, exactly like you requested:
x = complex(3,0)
x = 3.0000 + 0.0000i
isreal(x) % is complex
ans = logical
0
imag(x) % imaginary part is exactly zero (unlike the answer you accepted).
ans = 0
Torsten
Torsten 2024 年 4 月 29 日
This solution won't give me a cell array with representaion as zero in the imaginary number,
What cell array ? This is the translation of your code from above:
data = [1;2;3;4;5;6;7;8;9;10]
data = 10x1
1 2 3 4 5 6 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
data = complex(data,0)
data =
1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i 4.0000 + 0.0000i 5.0000 + 0.0000i 6.0000 + 0.0000i 7.0000 + 0.0000i 8.0000 + 0.0000i 9.0000 + 0.0000i 10.0000 + 0.0000i

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


Deepu
Deepu 2024 年 4 月 29 日
% Example double cell array with only real parts
double_cell_array = {1, 2, 3.5, 4};
% Convert to double complex cell array with zero imaginary parts
complex_cell_array = cellfun(@(x) [num2str(x) ' + 0i'], double_cell_array, 'UniformOutput', false);
% Display the resulting complex cell array
disp(complex_cell_array);

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by