MATLAB: Sum of consecutive elememts for loop homework suppose i have a matrix A=[1 2 3 4 5] and i need another matix as sum of consecutive elememts i.e [3 5 7 9] how to do using loop Best Answer Without loop: res=A(1:end-1)+A(2:end)With loop: for i=1:numel(A)-1 res(i)=A(i)+A(i+1); end Related SolutionsMATLAB: I have a matrix BB = [-1 5 6;4 -3 2;5 6 -7]. How can i print the location(index) of the positive elelments using for loop and using while loop BB = [-1 5 6;4 -3 2;5 6 -7] ;[nx,ny] = size(BB) ;for i = 1:nx for j = 1:ny if BB(i,j)>0 fprintf('%d %d %f\n',i,j,BB(i,j)) end endend MATLAB: WHILE LOOP program does not work Try it without loops, in a more MATLAB-ish vectorized way:indexesToSum = a>=4 & a<=15Re = sum(a(indexesToSum)) Related QuestionHow to sort an array in descending order without using the sort functionHow can I have all the for loop outputs in one matrixHow to generate matrix with one loopStoring values from a for loop into a new variableHow can i subtract the elements in a coloumn
Best Answer