Remove trailing zeros while making matrix
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi. I have two matrices for example a=[1,2;3,4] and b=[5+6i,6+7i] i made C=[A,B]. but result shows [1.0000 2.0000 5.0000+6.0000i] and the second row like this format too. I wanna delete trailling zeros and make matrix like [1 2 5+6i]. Thank You.
採用された回答
When combining real and compex numbers within a single array, the array will appear in compex format. Here are some ways to format real and complex numbers.
If you're using R2021a or later, you can use the formattedDisplayText function to capture the display output as a string (documentation) and write that to file rather than using diary.
Set format
You can set the display format to remove trailing zeros but the command window display will remain in complex format. If you're writing these variables to file or to a user interface component such as a text box, that's a different story. See sprintf() and fprintf() and let us know if you have any followup questions.
format shortg
a = [1; 2; 3];
b = [5+6i; 6+7i; 7+8i];
c = [a,b]
c =
1 + 0i 5 + 6i
2 + 0i 6 + 7i
3 + 0i 7 + 8i
format long
disp(c)
1.000000000000000 + 0.000000000000000i 5.000000000000000 + 6.000000000000000i
2.000000000000000 + 0.000000000000000i 6.000000000000000 + 7.000000000000000i
3.000000000000000 + 0.000000000000000i 7.000000000000000 + 8.000000000000000i
Use compose to specify formating of real and imaginary components
To print a line of text that formats real and complex values individually,
format shortg % not needed for the string conversion
a = [1, 2, 3];
b = [5+6i, 6+7i, 7+8i];
c = [a;b]
c =
1 + 0i 2 + 0i 3 + 0i
5 + 6i 6 + 7i 7 + 8i
% Convert all values to complex strings
complexStr = compose('%g%+gi', real(c(:)), imag(c(:)))
complexStr = 6×1 cell array
{'1+0i'}
{'5+6i'}
{'2+0i'}
{'6+7i'}
{'3+0i'}
{'7+8i'}
% Convert the real value strings
isReal = imag(c(:))==0;
complexStr(isReal) = compose('%g', real(c(isReal)))
complexStr = 6×1 cell array
{'1' }
{'5+6i'}
{'2' }
{'6+7i'}
{'3' }
{'7+8i'}
% Join all strings into 1
str = strjoin(complexStr', ', ')
str = '1, 5+6i, 2, 6+7i, 3, 7+8i'
% Or, display as cell array
cstr = reshape(complexStr, size(c))
cstr = 2×3 cell array
{'1' } {'2' } {'3' }
{'5+6i'} {'6+7i'} {'7+8i'}
Isolate the real and complex values within cell arrays
a = [1; 2; 3];
b = [5+6i; 6+7i; 7+8i];
c = [a,a,b]
c =
1 + 0i 1 + 0i 5 + 6i
2 + 0i 2 + 0i 6 + 7i
3 + 0i 3 + 0i 7 + 8i
out = num2cell(c)
out = 3×3 cell array
{[1]} {[1]} {[5 + 6i]}
{[2]} {[2]} {[6 + 7i]}
{[3]} {[3]} {[7 + 8i]}
out = [mat2cell(c(:,1:2), ones(size(c,1),1),2), num2cell(c(:,3))]
out = 3×2 cell array
{[1 1]} {[5 + 6i]}
{[2 2]} {[6 + 7i]}
{[3 3]} {[7 + 8i]}
7 件のコメント
armin m
2021 年 8 月 31 日
Yes that is right. I want to show in a text file without complex mode. Which prints from command windows in diary on mode
2. Is it possible to show two matrices together like:
Y matrix:
[2 4] [5+6i]
4 5 4+7i
OR
[2 4] = [5+6i]
4 5 4+7i
If you are scraping text from the command window, then the format option (short, shortg, etc) will remove the trailing 0s but it will still appear as complex.
If you only want the real or imaginary componenents,
format shortg
a = [1, 2, 3];
b = [5+6i, 6+7i, 7+8i];
c = [a;b]
c =
1 + 0i 2 + 0i 3 + 0i
5 + 6i 6 + 7i 7 + 8i
real(c)
ans = 2×3
1 2 3
5 6 7
imag(c)
ans = 2×3
0 0 0
6 7 8
You can combine the real and complex values into a single string by following this example (tweek it to your needs)
% Convert all values to complex strings
complexStr = compose('%g%+gi', real(c(:)), imag(c(:)))
complexStr = 6×1 cell array
{'1+0i'}
{'5+6i'}
{'2+0i'}
{'6+7i'}
{'3+0i'}
{'7+8i'}
% Convert the real value strings
isReal = imag(c(:))==0;
complexStr(isReal) = compose('%g', real(c(isReal)))
complexStr = 6×1 cell array
{'1' }
{'5+6i'}
{'2' }
{'6+7i'}
{'3' }
{'7+8i'}
% Join all strings into 1
str = strjoin(complexStr', ', ')
str = '1, 5+6i, 2, 6+7i, 3, 7+8i'
If you're using R2021a or later, you can use the formattedDisplayText function to capture the display output as a string (documentation) and write that to file rather than using diary.
Adam Danz
2021 年 8 月 31 日
I'll move my comment above to my answer since I think it more directly addresses your main question.
Very good thank you for your comment: but i like this formation:
{1,2} {5+6i}
{2,3} {5+7i}
.
.
.
Is it possible with your mentioned method?Yes, it's possible to create that exact output with a little bit of work.
Before digging into producing that exact output, you would need to explain the expected arrangement of real and complex numbers. For example, is your matrix is always 2x3 with the complex values only in the last column? If so, you can isolate the first two columns, put them in a cell array (see mat2cell demo below), and then append the last column to the cell array. Are you looking for numeric output or string output? Either way, you can take it from here.
The two examples below do not produce that exact output but come close to it.
Using strings,
format shortg
a = [1; 2; 3];
b = [5+6i; 6+7i; 7+8i];
c = [a,a,b]
c =
1 + 0i 1 + 0i 5 + 6i
2 + 0i 2 + 0i 6 + 7i
3 + 0i 3 + 0i 7 + 8i
complexStr = compose('%g%+gi', real(c(:)), imag(c(:)));
isReal = imag(c(:))==0;
complexStr(isReal) = compose('%g', real(c(isReal)));
reshape(complexStr, size(c))
ans = 3×3 cell array
{'1'} {'1'} {'5+6i'}
{'2'} {'2'} {'6+7i'}
{'3'} {'3'} {'7+8i'}
Without using strings,
num2cell(c)
ans = 3×3 cell array
{[1]} {[1]} {[5 + 6i]}
{[2]} {[2]} {[6 + 7i]}
{[3]} {[3]} {[7 + 8i]}
Using mat2cell & num2cell
out = [mat2cell(c(:,1:2), ones(size(c,1),1),2), num2cell(c(:,3))]
out = 3×2 cell array
{[1 1]} {[5 + 6i]}
{[2 2]} {[6 + 7i]}
{[3 3]} {[7 + 8i]}
armin m
2021 年 8 月 31 日
Thank you very much.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
