split a vector and save it as a cell (1xN)

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2023년 7월 13일
댓글: Dyuman Joshi 2023년 7월 14일
I have three vectors:
  • vector_1 has numbers from 69 to 98;
  • vector_2 has numbers from 69 to 78 and 82 to 98;
  • vector_3 has numbers 69 to 78, 82 to 89 and 91 to 98.
I would like to subdivide the vector into a cell:
  • for vector_1: I need to get a cell with numbers 69 to 98 (cell 1x1);
  • for vector_2: I need to get a cell with the numbers 69 to 78 and 82 to 98 (cell 1x2);
  • for vector_3: I need to get a cell with the numbers 69 to 78, 82 to 89 and 91 to 98 (cell 1x3).
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 7월 14일
"Is there any possibility to create a more generic code?"
The data you have is stored dynamically, which is hard to work with. There is possibility of improvement from the given answer, but you will have to do the same thing for each vector manually.
You can store the data in a cell array and run a for loop through each element.
How many vectors are you dealing with? What do you want to do this the output?

댓글을 달려면 로그인하십시오.

채택된 답변

Vishnu
Vishnu 2023년 7월 13일
To subdivide avector into a cell array you can use the following code snippet. In this code, the cellArray is initialized as a cell array with the appropriate size to store the subdivisions of each vector. The subdivisions are then assigned to the respective cells using indexing.
% Define the vectors
vector_1 = 69:98;
vector_2 = [69:78, 82:98];
vector_3 = [69:78, 82:89, 91:98];
% Create a cell array to store the subdivisions
cellArray = cell(1, 2);
% Subdivide vector_1
cellArray{1} = vector_1;
% Subdivide vector_2
cellArray{2} = [vector_2(1:10), vector_2(15:end)];
% Subdivide vector_3
cellArray{3} = [vector_3(1:10), vector_3(15:22), vector_3(24:end)];
  댓글 수: 4
Alberto Acri
Alberto Acri 2023년 7월 13일
Thank you, I managed to find the solution.
Vishnu
Vishnu 2023년 7월 13일
No problem Alberto could you mark this answer as acceped then?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by