MATLAB: How to find sum using single statement sum I had to find the sum of the series 1/2+(1/2)^2+….(1/2)^100 why does my code not work? (ones(1,100).*1/2).^([1:100]) it gives the answer zero. Best Answer Airas, use f = sum(arrayfun(@(x) 0.5^x,1:100))or, with your approach, add the sum() f = sum((ones(1,100).*1/2).^([1:100])) Related SolutionsMATLAB: Is the sum outputting a curly bracket You are attempting to combine strings and numeric data. That will not work. Try this: disp(['Sum of even numbers from 2 to 200 =',num2str(sum(2:2:200))])The best option is likely sprintf or fprintf. MATLAB: Finding rows in a matrix Another:% The given matrixA = [1 2; 2 1; 1 2; 2 2; 1 1; 2 2];% Now find the counts.[I,J,K] = unique(sort(A,2),'rows'); % I has the unique rows.C = histc(K,1:max(K)); % This has the corresponding counts.% Now that we have found the counts, display them:fprintf('Row [%i %i] appears %i times. \n',[I C]')If you want to only get the counts for the one type, this will do it quickly:cnt = sum(all(bsxfun(@eq,sort(A,2),[1,2]),2)); Related QuestionHelp with if-else statementHOW TO Summation of FunctionsHow to use ‘goto’ statement
Best Answer