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

6 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2015-3-20
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.
Stephen23
Stephen23 2015-3-20
编辑:Stephen23 2015-3-20
@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:

请先登录,再进行评论。

回答(3 个)

Guillaume
Guillaume 2015-3-19
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

Image Analyst
Image Analyst 2015-3-19
A = xlsread('workbook3.xls');
% Get the second column multiplied by your number.
secondColumn = yourNumber * A(:,2);
% Get mean of that
meanOfSecondColumn = mean(secondColumn(:));

Shantanu Jana
Shantanu Jana 2015-3-19
编辑:Shantanu Jana 2015-3-20
%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 个评论
Guillaume
Guillaume 2015-3-19
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.
Stephen23
Stephen23 2015-3-20
编辑:Stephen23 2015-3-20
@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:

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by