i have this assigment:
Create a function that computes the cost of each item and returns it as a vector. Find an elegant and simple way to write the code using what you have learned about matrix and vector operations.
here's what i have come up with so far which is a not so elegant solution and only works for a specific size of matrix. in my script it has have more than 1 column and less than 4 columns:
function itemCost=computeItemCost(resourceItemMatrix,resourceCost)if length(resourceItemMatrix)>1 A=sum(resourceItemMatrix(:,1).*reshape(resourceCost,length(resourceCost),1)); itemCost=A; if length(resourceItemMatrix)>1 B=sum(resourceItemMatrix(:,2).*reshape(resourceCost,length(resourceCost),1)); C=sum(resourceItemMatrix(:,3).*reshape(resourceCost,length(resourceCost),1)); itemCost=[A,B,C]; if length(resourceItemMatrix)>3 D=sum(resourceItemMatrix(:,4).*reshape(resourceCost,length(resourceCost),1)); itemCost=[A,B,C,D]; end endendend
I know there is a much simpler way to do this(perhaps a for-loop?), but just keep running my head against a wall when i try. help would be much appreciated.
Best Answer