필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
Kirsty Strachan
Kirsty Strachan 2015년 3월 19일
편집: Stephen23 2015년 3월 20일
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