MATLAB: All possible combinations of 2 vectors. combinations Hi everyone. I have one vector and one number. For example [1 3 5] and 0. How do I generate all possible combinations? Like this: 0 3 5 1 0 5 1 3 0 0 0 5 0 3 0 1 0 0 0 0 0 Best Answer Here is a solution:function H = mycomb(V)% HelpL = length(V);H = cell(1,L);for ii = 1:L-1 C = nchoosek(1:L,L-ii); R = cumsum(ones(size(C))); M = max(R(:,1)); H{ii} = zeros(M,L); H{ii}(R+(C-1)*M) = V(C);endH{L} = zeros(1,L);H = vertcat(H{:});Now try it out from the command line:>> mycomb([4 5 6])ans = 4 5 0 4 0 6 0 5 6 4 0 0 0 5 0 0 0 6 0 0 0 >> mycomb([4 5 6 7])ans = 4 5 6 0 4 5 0 7 4 0 6 7 0 5 6 7 4 5 0 0 4 0 6 0 4 0 0 7 0 5 6 0 0 5 0 7 0 0 6 7 4 0 0 0 0 5 0 0 0 0 6 0 0 0 0 7 0 0 0 0 Related SolutionsMATLAB: Centre of a matrix A = [0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 4 5 6 7 1 2 0 00 0 2 0 0 0 3 8 0 00 0 4 0 0 0 7 6 0 00 0 4 5 7 5 8 5 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0];out = A(any(A,2),any(A));out = out(~all(out,2),~all(out));ort = bwlabel(~A);t = max(t(:)) == t;out = A(any(t,2),any(t)); MATLAB: Using a loop and counter for parametric equations HiHere is the solution of your task:function E = realED(H, T, h)%%%rho=1030;g=9.81;%%L = zeros(numel(H), numel(T), numel(h));k=L;Cg=L;E=L;for ii=1:numel(H) for m=1:numel(T) for jj = 1:numel(h) L(ii, m, jj) = (g*T(m)^2)/(2*pi*tan(h(jj)))*((4*pi)^2)*h(jj)/(g*T(m)^2)^3; k(ii, m, jj)=2*pi/L(ii, m, jj); Cg(ii, m, jj) = .5*((1+((2*k(ii, m, jj)*h(jj))/sin(h(jj))))*2*k(ii,m,jj)*h(jj))*L(ii, m, jj)/T(m); E(ii, m, jj) = ((rho*g)/16)*(H(ii)^2)*Cg(ii, m, jj); end endendGood luck Related QuestionHow to save matrix created in each iterationVectorization – problem with zerosPre allocating triple loops
Best Answer