False size for row vector
古いコメントを表示
I would like to create the row vector T1=[1 2 4 8 14 15 9 16 17 5 10 18 19 1 21 20 3 6 12 13 7] with one column 1-21, which is seperated in column 1-19 and 20-21 but I always get the column 1-17 and 18-21.
What can I do to get the right sizing?
9 件のコメント
Steven Lord
2024 年 1 月 10 日
I don't understand what you're asking. There doesn't seem to be any rhyme or reason to the ordering of the elements in T1, and I'm not sure what you mean by "is separated in column 1-19". Do you perhaps mean you want the Command Window to display columns 1 through 19 of T1 on one line then display columns 20 through the end on the next line?
Sveva
2024 年 1 月 10 日
Sveva
2024 年 1 月 10 日
Sveva
2024 年 1 月 10 日
Steven Lord
2024 年 1 月 10 日
Note that what you see in the Command Window only affects how the vector is displayed. It doesn't change the actual size of the vector at all, if that's part or all of your concern. You can display a 20 element vector with a Command Window that's just wide enough for 2 elements per row or wide enough for 200 elements per row. It doesn't change the fact that the vector is stored with 20 elements.
I've seen plenty of questions where people thought changes to how data in MATLAB is displayed also changed how it is stored, and that's not usually the case. Calling round or reshape, for example, would change the stored data or the stored size of the array and those changes would impact how it is displayed. But changing the size of the Command Window (to show more or fewer elements per line) or using the format function (to change how many decimal places are displayed) are cosmetic changes only.
With the standard display? No, not really. You have some control of the display format but that doesn't give you fine-grained control ("I want exactly N elements per line", for example.)
disp(0.5+(1:100))
format long % long fixed point format
format compact % suppress excess blank lines
disp(0.5+(1:100))
With a command like fprintf? Sure, as long as you can construct the format string to your specifications. Note that the format string in this example does not contain a newline, so fprintf prints it all as one long line of text.
fprintf('%g\t', 0.5+(1:100))
If I wanted to display this in lines of say 60 characters, I could do that using sprintf to create the text then textwrap to wrap it.
s = sprintf('%g ', 0.5+(1:100));
s2 = string(textwrap({s}, 60))
strlength(s2)
Torsten
2024 年 1 月 10 日
That's helpful. Many thanks @Steven Lord
回答 (1 件)
Ayush Anand
2024 年 1 月 10 日
1 投票
Hi Sveva,
The MATLAB command window has a default width that determines how many columns of a vector it will display on one line before wrapping around to the next line. If you're seeing the vector T1 split into two lines with the first line showing elements 1-17 and the second line showing elements 18-21, this is due to the command window's width settings.
MATLAB automatically formats the output to fit within the command window's width. You cannot directly control where the line break occurs in the command window's display of a long vector. However, you can manually drag and resize the command window width to change where the break occurs. A wide enough command window will display all the elements in one row, you can do trial and error to resize the window to get columns 1-19 and 20-21.
I hope this helps!
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!