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

1 次查看(过去 30 天)
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 ;
....]

采纳的回答

Bruno Luong
Bruno Luong 2019-7-16
编辑:Bruno Luong 2019-7-16
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by