MATLAB: Elements processing in array array If array A=[ b c ], I want to create value [b+c -b+c b-c -b-c] or A=[ b c d ], then output should be [ b+c+d b+c-d b-c+d b-c-d -b+c+d -b+c-d -b-c+d -b-c-d ] Is there any function in MATLAB that can easily solve my problem ? Thanks. Best Answer You basically want the sum of the n-ary product of the +/- sets of each element of your array. That can be done easily with ndgrid:A = [1 3 8 2]; %demo data;sets = num2cell([A; -A], 1); %split into sets of +/- valuesnprod = cell(1, numel(A)); %create destination for n-ary product[nprod{:}] = ndgrid(sets{:}); %calculate n-ary productnprod = reshape(cat(numel(A)+1, nprod{:}), [], numel(A)); %concatenate into one matrix and reshape. Each row is one of the possible combinationsumnprod = sum(nprod, 2) %wanted result Related SolutionsMATLAB: Fast Element-Wise power a=1:10000000; b=repmat(3,size(a)); tic c1=a.^b; toc tic c2=a.^3; toc tic c3=a.*a.*a; tocAnd then the results are, Elapsed time is 0.118509 seconds. Elapsed time is 0.129281 seconds. Elapsed time is 0.006726 seconds. MATLAB: Multiply then sum elements of two matrices It is not clear from your question whether corresponding elements are to be multiplied or if you are wanting to do a matrix multiplication.If corresponding elements are to be multiplied, then you could calculate the sum-of-products usingdot(A(:),B(:)) Related QuestionHow to create a 2^n by n matrix that the rows of the matrix enumerate all the possibilities for a n digit binary numberHow to generate a single vector of block-consecutive values from 2 vectors of same size without a loopPad a vector to get matrixPut elements into corresponding locations of upper triangular matrixHow to avoid ‘two for loop’
Best Answer