Re-representing an int list

6 views (last 30 days)
Yinan Xuan
Yinan Xuan on 1 May 2018
Commented: Walter Roberson on 1 May 2018
Hi,
I have a super long list like [1 2 3 5 6 7 500 501 503 ... 11589 11590 11591]. I am wondering if I can visualize it in a way like [1:3,5:7,500:503,...,11589:11591]. If I cannot do it in matlab, can I do it in python?
Thank you very much!
  2 Comments
Guillaume
Guillaume on 1 May 2018
If you write [1:3, 5:7] at the command line it will be automatically displayed as
>> [1:3, 5:7]
ans =
1 2 3 5 6 7
i.e. automatically expanded. I'm not sure if that's what you mean by visualize as there are many different ways to look at the content of a variable in matlab, but if you mean at the command line, no that is not possible.
Note that while there is only one expanded way of displaying the array, there is an infinite way of condensing it.
However, on a related note, see this cody problem I posted several years ago.
Walter Roberson
Walter Roberson on 1 May 2018
I have posted code for this in the past, but unfortunately it is probably lost in the masses of other things I have posted.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 1 May 2018
If your list is a vector of integers from 1 to 11591, with a length of 11591, try this:
List = 1:11591;
N = numel(List);
M = nan(1,ceil(N/3)*3);
M(1:N) = List;
M = reshape(M, 3, [])';
The last element of the ‘M’ matrix is NaN, because the number of elements in ‘M’ must be an integer multiple of the number of columns you want in it (here 3) in order for the reshape function to work.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!