MATLAB: How to sum of each row according to values values in given function

sum of columns

Here I have a matrix of five rows and six columns. I want sum of each row according to given function by using for loop.
f(1)=15*x(1)+20*x(2)+30*x(3)+25*x(4)+40*x(5)+35*x(6);
f(2)=20*x(1)+23*x(2)+21*x(3)+24*x(4)+28*x(5)+27*x(6);
f(3)=16*x(1)+24*x(2)+22*x(3)+23*x(4)+20*x(5)+17*x(6);
f(4)=25*x(1)+26*x(2)+27*x(3)+28*x(4)+29*x(5)+30*x(6);
where x shows the element number in each row, such as six columns in below matrix
1 0 1 0 0 1
1 0 0 1 0 1
1 0 0 0 1 1
1 0 0 1 1 1
0 1 1 0 0 1

Best Answer

You can do something like the code below. You could use a for loop instead of the call to cellfun, but as you can see, you don't really need an explicit loop here.
data=[1 0 1 0 0 1
1 0 0 1 0 1
1 0 0 0 1 1
1 0 0 1 1 1
0 1 1 0 0 1 ];
data_cell=mat2cell(data,ones(1,size(data,1)),size(data,2)); %group data by row
f_cell=cellfun(@MyFunction,data_cell,'UniformOutput',0); %apply function to each row
f=cell2mat(f_cell); %convert results back to an array
function f=MyFunction(x)
f(1)=15*x(1)+20*x(2)+30*x(3)+25*x(4)+40*x(5)+35*x(6);
f(2)=20*x(1)+23*x(2)+21*x(3)+24*x(4)+28*x(5)+27*x(6);
f(3)=16*x(1)+24*x(2)+22*x(3)+23*x(4)+20*x(5)+17*x(6);
f(4)=25*x(1)+26*x(2)+27*x(3)+28*x(4)+29*x(5)+30*x(6);
end