MATLAB: Subtracting a value from a vector matrix subtraction Hello I have a 10*1 vector like A=[3,4,5,6,7,8,9,2,4,3]. I want to subtract all the elements of this vector by another 1*1 vector B=[5]. A-B doesn't work to give me [-2,-1,0,1,2,3,4,-1,-2]. How can I do this? Best Answer Maybe you mean: how to remove the element in B from AA=[3,4,5,6,7,8,9,2,4,3]B=3out=setdiff(A,B) Related SolutionsMATLAB: How to modify a matrix to change the entries >=0 and multiply it by 5 AND to change the entries <0 and add 2 to those entries Start with "If an entry in B is greater than or equal to zero": You can either run a loop over all elements of B or use "logical indexing":% Linear indexing:for k = 1:numel(B) if B(k) >= 0 ... endendOr two indices for rows and columns:for r = 1:size(B, 2) for c = 1:size(B, 1) if B(r, c) >= 0 ... end endendOr "logical indexing":index = (B >= 0);disp(B(index))Now try to use this to modify B. Use the mentioned terms to search in the documentation and in the internet. MATLAB: How to make a matrix with all possible combination >> [A,B] = ndgrid(-2:2);>> [A(:),B(:)]ans = -2 -2 -1 -2 0 -2 1 -2 2 -2 -2 -1 -1 -1 0 -1 1 -1 2 -1 -2 0 -1 0 0 0 1 0 2 0 -2 1 -1 1 0 1 1 1 2 1 -2 2 -1 2 0 2 1 2 2 2For a general solution, try this:>> n = 3;>> C = cell(1,n);>> [C{:}] = ndgrid(-n:n);>> C = cellfun(@(m)m(:),C,'uni',0);>> [C{:}]ans = -3 -3 -3 -2 -3 -3 -1 -3 -3 0 -3 -3 1 -3 -3 2 -3 -3 3 -3 -3 -3 -2 -3 -2 -2 -3 -1 -2 -3... lots of lines here 2 2 3 3 2 3 -3 3 3 -2 3 3 -1 3 3 0 3 3 1 3 3 2 3 3 3 3 3 Related QuestionFind missing element between two matrixHelp with removing duplicate sub-elements….How to select random numbers from array for creating new matrix
Best Answer