Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
Kirsty Strachan
Kirsty Strachan am 19 Mär. 2015
Bearbeitet: Stephen23 am 20 Mär. 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 Kommentare
Image Analyst
Image Analyst am 20 Mär. 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 am 20 Mär. 2015
Bearbeitet: Stephen23 am 20 Mär. 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:

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Guillaume
Guillaume am 19 Mär. 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 am 19 Mär. 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 am 19 Mär. 2015
Bearbeitet: Shantanu Jana am 20 Mär. 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 Kommentare
Guillaume
Guillaume am 19 Mär. 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 am 20 Mär. 2015
Bearbeitet: Stephen23 am 20 Mär. 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:

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by