Remove trailing zeros while making matrix
29 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Adam Danz
on 31 Aug 2021
Edited: Adam Danz
on 31 Aug 2021
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]
format long
disp(c)
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]
% Convert all values to complex strings
complexStr = compose('%g%+gi', real(c(:)), imag(c(:)))
% Convert the real value strings
isReal = imag(c(:))==0;
complexStr(isReal) = compose('%g', real(c(isReal)))
% Join all strings into 1
str = strjoin(complexStr', ', ')
% Or, display as cell array
cstr = reshape(complexStr, size(c))
Isolate the real and complex values within cell arrays
a = [1; 2; 3];
b = [5+6i; 6+7i; 7+8i];
c = [a,a,b]
out = num2cell(c)
out = [mat2cell(c(:,1:2), ones(size(c,1),1),2), num2cell(c(:,3))]
7 Comments
Adam Danz
on 31 Aug 2021
Edited: Adam Danz
on 31 Aug 2021
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]
complexStr = compose('%g%+gi', real(c(:)), imag(c(:)));
isReal = imag(c(:))==0;
complexStr(isReal) = compose('%g', real(c(isReal)));
reshape(complexStr, size(c))
Without using strings,
num2cell(c)
Using mat2cell & num2cell
out = [mat2cell(c(:,1:2), ones(size(c,1),1),2), num2cell(c(:,3))]
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!