What is the best way to display serial numbers in Matlab?

For example, I have an array:

a=[1:5 8:10];

If I show it with:

disp(['a = ' num2str(a)]);

The result would be something like

a = 1 2 3 4 5 8 9 10

It is too long than I need. How can I let Matlab appear just like me, or as close as that?

More specifically, if I defined the variable in an “informal” way, for example:

a=[1:3 4:6 8:10]

(usually should be 1: 6 instead of 1: 3 4: 6)

I just want Matlab to display anyway:

1:3 4:6 8:10    or    1:6 8:10

I also don't care if it displays the variable name or square brackets.

Searched, but did not find anything useful. Considered for manual analysis, but this does not seem like a smart way.

Any suggestion would be very helpful, thanks a lot.

+3
source share
3 answers

- . , , ​​:

function display_array(array)
    str = cellfun(@(n) {num2str(n)}, num2cell(array));
    index = (diff(array) == 1) & ([1 diff(array, 2)] == 0);
    str(index) = {':'};
    str = regexprep(sprintf(' %s', str{:}), '( :)+\s*', ':');
    disp([inputname(1) ' = [' str(2:end) ']']);
end

:

>> a = [1:5 7 9:11]  %# Define a sample array

a =

     1     2     3     4     5     7     9    10    11     %# Default display

>> display_array(a)
a = [1:5 7 9:11]     %# Condensed display
>> b = [1 2 3 4 4 4 3 2 1];  %# Another sample array
>> display_array(b)
b = [1:4 4 4 3 2 1]  %# Note only the monotonically increasing part is replaced
+5

vec2str Matlab. :

str = vec2str([1 3 5 5 9 8 7 6 5]) 
ans = 
    [1:2:5,5,9:-1:5] 

>> eval(str) 
ans = 
     1 3 5 5 9 8 7 6 5 
+3

impossible. Matlab very quickly drops your definition. An “object” adoes not know this definition at all.

0
source

All Articles