Re-representing an int list

6 次查看(过去 30 天)
Yinan Xuan
Yinan Xuan 2018-5-1
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 个评论
Guillaume
Guillaume 2018-5-1
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 2018-5-1
I have posted code for this in the past, but unfortunately it is probably lost in the masses of other things I have posted.

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2018-5-1
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by