Storing vectors of different lengths

47 visualizaciones (últimos 30 días)
Scott Banks
Scott Banks el 6 de Oct. de 2025 a las 19:00
Movida: dpb el 8 de Oct. de 2025 a las 16:16
Dear Everyone,
Just a quick question:
I have 4 vectors, each of different lengths, and I want to store them for some calculations later on in my script.
So I tried:
Hx = [36 72 108 144] % Height of building
v = Hx./3*15 % Maximum shear force acting on the building
V = flip((15:15:v)) % 4 vectors containing the cumulative shear forces at each storey
But colon operators must be real scalars. So, this won't work.
My question is: what is the simplest way to obtain and store the 4 vectors I want of varying lengths?
Many thanks to you all.
Scott
  1 comentario
Scott Banks
Scott Banks el 8 de Oct. de 2025 a las 15:50
Movida: dpb el 8 de Oct. de 2025 a las 16:16
Thank you, guys, that's great!

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 6 de Oct. de 2025 a las 19:24
Movida: dpb el 6 de Oct. de 2025 a las 19:25
Hx = [36 72 108 144] % Height of building
Hx = 1×4
36 72 108 144
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = Hx./3*15 % Maximum shear force acting on the building
v = 1×4
180 360 540 720
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
V = arrayfun(@(h)[15:15:h].',v,'uni',0);
maybe is what you had in mind?
The above will be the same for each up to the preceding length since set a fixed lower limit and step size for each; maybe you intended to use a zero for the first and then the height of the previous story for the base of the next?
That would be something like
V = arrayfun(@(f,c)[f:15:c].',[0 v(1:end-1)],v,'uni',0)
V = 1×4 cell array
{13×1 double} {13×1 double} {13×1 double} {13×1 double}
  3 comentarios
Torsten
Torsten el 6 de Oct. de 2025 a las 21:25
Editada: Torsten el 6 de Oct. de 2025 a las 21:28
V = arrayfun(@(h)flipud([15:15:h].'),v,'uni',0)
or
V = arrayfun(@(f,c)flipud([f:15:c].'),[0 v(1:end-1)],v,'uni',0)
Stephen23
Stephen23 el 7 de Oct. de 2025 a las 4:58
Editada: Stephen23 el 7 de Oct. de 2025 a las 5:04
Without the misleading square brackets:
Hx = [36,72,108,144];
v = Hx./3*15;
V = arrayfun(@(h)flip(15:15:h),v, 'uni',0);
Checking:
V{:}
ans = 1×12
180 165 150 135 120 105 90 75 60 45 30 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×24
360 345 330 315 300 285 270 255 240 225 210 195 180 165 150 135 120 105 90 75 60 45 30 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×36
540 525 510 495 480 465 450 435 420 405 390 375 360 345 330 315 300 285 270 255 240 225 210 195 180 165 150 135 120 105
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×48
720 705 690 675 660 645 630 615 600 585 570 555 540 525 510 495 480 465 450 435 420 405 390 375 360 345 330 315 300 285
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Iniciar sesión para comentar.

Más respuestas (1)

Umar
Umar el 7 de Oct. de 2025 a las 5:26

Hi @Scott Banks,

I saw your question about generating four vectors of varying lengths for building shear forces. I put together a clean MATLAB approach that does exactly what you need.

%% Building heights and max shear
Hx = [36, 72, 108, 144];
v  = Hx/3*15;
step = 15;
%% Generate all descending vectors in a single, compact line
V = arrayfun(@(x) linspace(x, step, ceil(x/step)), v, 'UniformOutput', false);
%% Display each vector
cellfun(@(vec,i) fprintf('Building %d | H = %d m | Shear: %s\n', i, Hx(i),   strjoin(string(vec), ' ')), V, num2cell(1:numel(V)));

Results: please see attached.

How it works:

  • Each element of `V` is a descending vector from the maximum shear down to 15 kN.
  • The vectors automatically have the correct length for each building.
  • All vectors are stored in a cell array, making them easy to access later (`V{1}`, `V{2}`, etc.).
  • No loops are needed — the code is concise, readable, and scalable.

This approach directly gives you the four vectors you wanted, in a neat, structured way. It’s also easy to extend if you add more buildings or change the step size.

Hope this helps!

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by