How can I create a matrix with combination of some other matrix elemenst dynamicaly in a loop

1 visualizzazione (ultimi 30 giorni)
Dear Friends
Need an emergency help. it is a pleasure to get any ideas to solve the problem.
discription:
I have a dynamic code which generate some vectors with different size of members like bellow:
The counter is vary from 2 to a unknown value
in loop 1, I have V1, V2 where V1=( 1 2 3), V2=(4, 5)
and Now I want to create a matrix M=[ 1 4;1 5; 2 4; 2 5; 3 4; 3 5]
in the loop 2 the other vector as V3 =(6 7 8) is added to the sysytem and now I want new
M=[ 1 4 6;
1 4 7;
1 4 8;
1 5 6;
1 5 7;
....
3 5 7;
3 5 8]
In the next loop V4 is generrate and if V4=( 9 10) new M should be the combination of all elements of V1, V2, V3, V4
M=[ 1 4 6 9;
1 4 6 10 ;
....]

Risposta accettata

Bruno Luong
Bruno Luong il 16 Lug 2019
Modificato: Bruno Luong il 16 Lug 2019
V = {[1 2 3] [4 5] [6 7 8] [9 10]};
M = zeros(1,0);
n = cell(0);
for k=1:length(V)
Vk = V{k}; % replace by your dynamic generation
Vk = Vk(:);
m = size(M,1);
n{k} = 1;
n{1} = size(Vk,1);
M = [repelem(M, n{:}), repmat(Vk,[m 1])];
% do something with the updated M ...
M
end

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by