Vector Manipulation - How to achieve this specific form?

Hi all,
I want to receive an expression like this: sos = {[1 8761]' [2 8762]' [3 8763]' [4 8764]' ... [8760 17520]'};
Do you have an idea on how to achieve this?

 Respuesta aceptada

Try this:
v1 = 1:17520; % Create Vector
v2 = reshape(v1, [], 2)'; % Reshape Vector To 2-Row Matrix
c = mat2cell(v2, 2, ones(1,size(v2,2))); % Create Cell Array
q1 = v2(:,1:5); % View Original Matrix Sample (Delete Later)
q2 = [c{1:5}] % View Cell Array Sample (Delete Later)
q2 =
1 2 3 4 5
8761 8762 8763 8764 8765
I created separate assignments for clarity. Note that ‘c’ is a (1x8760) cell array, the format requested.

4 comentarios

What would I have to change with c if v2 is defined as:
c = mat2cell(v2, 2, ones(1,size(v2,2)));
(So that the elements are [1,8761]... and not [1;8761])
That simply requires removing the transpose operator from ‘v2’, and in mat2cell, changing the second size argument, and changing the order of the last 2 arguments:
v1 = 1:17520; % Create Vector
v2 = reshape(v1, [], 2); % Reshape Vector To 2-Row Matrix
sos = mat2cell(v2, ones(1,size(v2,1)), 2); % Create Cell Array
q1 = v2(1:5,:); % View Original Matrix Sample (Delete Later)
q2 = [sos{1:5}] % View Cell Array Sample (Delete Later)
Note that ‘q1’ remains accurate, although ‘q2’ now horizontally concatenates the individual vectors, so it is not as interesting. The ‘sos’ cell array (previously ‘c’) has the desired format, that being (8760x1) with each cell a (1x2) vector instead of (1x8760) and (2x1) previously.
Perfect! Thanks a lot!!
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Mathias Dirksmeier
Mathias Dirksmeier el 1 de Oct. de 2018
Well, I surely know num2cell...
However, that wasn't the question, was it?
{[1 8761]' [2 8762]' [3 8763]' [4 8764]' ... [8760 17520]'};
How to achieve this specific form?

Categorías

Productos

Versión

R2018a

Preguntada:

el 1 de Oct. de 2018

Comentada:

el 2 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by