Effacer les filtres
Effacer les filtres

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

6 vues (au cours des 30 derniers jours)
Kirsty Strachan
Kirsty Strachan le 19 Mar 2015
Modifié(e) : Stephen23 le 20 Mar 2015
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 commentaires
Image Analyst
Image Analyst le 20 Mar 2015
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 le 20 Mar 2015
Modifié(e) : Stephen23 le 20 Mar 2015
@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:

Connectez-vous pour commenter.

Réponses (3)

Guillaume
Guillaume le 19 Mar 2015
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 le 19 Mar 2015
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 le 19 Mar 2015
Modifié(e) : Shantanu Jana le 20 Mar 2015
%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 commentaires
Guillaume
Guillaume le 19 Mar 2015
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 le 20 Mar 2015
Modifié(e) : Stephen23 le 20 Mar 2015
@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:

Connectez-vous pour commenter.

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by