How to multiply the second column of an array by a specified integer?

I need to multiply the second column of an excel file by a number and then find the mean of the new column. So far I have only worked out how to get matlab to read the excel file by:
A = xlsread('workbook3.xls');
Can anyone tell me how I would go about doing this?

4 Comments

We did. The only difference is that Guillaume's answer changes A while mine does not. Accept whichever way is the way you want it to happen.
you can call mean.m or you can write it in your way.but when you are learning it's better to define the function and it is more flexible then calling built in functions. accept which ever you like
Kirsty, if you use Shantanu's code, be sure to rename "sum" to something else, like "theSum" so that you don't destroy the built in sum() function.
@Kirsty Strachan: you should never call a function or variable the same name as an inbuilt function: this is a very bad idea that will produce all sorts of unpredictable side-effects. We regularly get questions here asking "why does function X not work?", when the user has themselves redefined X to be another function or variable.
Regardless of what Shantanu Jana has written in their answer and comment, you should not name variables sum or mean, or name your own function mean. As Image Analyst suggests, you can easily create your own unique function names. You can use which to check if a name is already defined.
Shadowing of functions is officially discouraged by MATLAB themselves:
And the practice comes in at number seven on this list of bad MATLAB coding practices:
There have been several discussions on the topic before:

Sign in to comment.

Answers (3)

Sigh. I don't really want to give the solution to such a trivial problem that is probably some homework, but I don't want the only answer to this question be one that shows the worst way of doing this in matlab.
Going through matlab's basic tutorial shows exactly how to do that.
A = xlsread('workbook3.xls')
A(:, 2) = A(:, 2) * 5; %multiply column 2 by 5
m = mean(A(:, 2)); %get the mean
A = xlsread('workbook3.xls');
% Get the second column multiplied by your number.
secondColumn = yourNumber * A(:,2);
% Get mean of that
meanOfSecondColumn = mean(secondColumn(:));
%this will multiply second column by number and store it in third column and store the mean in fourth %column
A = xlsread('workbook3.xlsx');
number=3;
sum=0;
p=size(A);
for i =1:p(1,1)
A(i,3)=A(i,2)*number;
sum=sum+A(i,3);
end
mean=sum/p(1,1);
A(1,4)=mean;
xlswrite('workbook3.xlsx',A)

2 Comments

Sometimes, I wish there was a way to downvote some answers.
Using a for loop for this is really bad advice. It's a trivial operation in matlab to multiply a column by a constant, just one line required. And it's also trivial to get its mean.
@Kirsty Strachan: you should never call a function or variable the same name as an inbuilt function: this is a very bad idea that will produce all sorts of unpredictable side-effects. We regularly get questions here asking "why does function X not work?", when the user has themselves redefined X to be another function or variable.
Regardless of what Shantanu Jana has written in their answer and comment, you should not name variables sum or mean, or name your own function mean. As Image Analyst suggests, you can easily create your own unique function names. You can use which to check if a name is already defined.
Shadowing of functions is officially discouraged by MATLAB themselves:
And the practice comes in at number seven on this list of bad MATLAB coding practices:
There have been several discussions on the topic before:

Sign in to comment.

Categories

Asked:

on 19 Mar 2015

Edited:

on 20 Mar 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!