Storing vectors of different lengths
47 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Scott Banks
am 6 Okt. 2025 um 19:00
Verschoben: dpb
am 8 Okt. 2025 um 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 Kommentar
Akzeptierte Antwort
dpb
am 6 Okt. 2025 um 19:24
Verschoben: dpb
am 6 Okt. 2025 um 19:25
Hx = [36 72 108 144] % Height of building
v = Hx./3*15 % Maximum shear force acting on the building
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)
3 Kommentare
Weitere Antworten (1)
Umar
am 7 Okt. 2025 um 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!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!