Re-representing an int list

조회 수: 6 (최근 30일)
Yinan Xuan
Yinan Xuan 2018년 5월 1일
댓글: Walter Roberson 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by