필터 지우기
필터 지우기

Where is my mistake in coding ?

조회 수: 1 (최근 30일)
Dung Le
Dung Le 2016년 1월 5일
댓글: Image Analyst 2016년 1월 5일
I separate input data (in file panel.xlsx) into groups based on 4 conditions (in file criteria.xlsx), namely:
  1. Province (64 values)
  2. Number of employees (3 ranges): 2-10; 11-49; 50-10000
  3. Industry ID (89 values)
  4. Year (2 ranges): 2005-2007; 2008-2010
Then i want to calculate mean for each group and assign this mean to every observation in each group. Above is my code, could you please point out what's wrong in the code and fix it. Thank you very much! And i already attach two files used for calculation.
clear all
clc
A=xlsread('criteria.xlsx');
B=xlsread('panel.xlsx');
for i=1:89
for j=1:64
for k=1:3
for h=1:2
rowsToAverage = (B(:,5) == A(i,1)) && (B(:,4) == A(j,2)) && (A(k,4) > B(:,2) > A(k,3)) && (A(h,5) < B(:,1) < A(h,6)) && ~isnan(B(:,3));
meanOfBigNumbers= mean(B(rowsToAverage, 3));
B(rowsToAverage, 6) = meanOfBigNumbers;
end
end
end
end
xlswrite('panel.xlsx',B)
  댓글 수: 3
Dung Le
Dung Le 2016년 1월 5일
편집: Walter Roberson 2016년 1월 5일
I'm sorry, I have just attached two related files. Matlab reports error in the line:
rowsToAverage = (B(:,5) == A(i,1)) && (B(:,4) == A(j,2)) && (A(k,4) > B(:,2) > A(k,3)) && (A(h,5) < B(:,1) < A(h,6)) && ~isnan(B(:,3));
I'm don't know how to foxed this code!
Stephen23
Stephen23 2016년 1월 5일
편집: Stephen23 2016년 1월 5일
Yes, but what is the error message? We cannot read your mind or your computer screen, so you have to tell us what the error message is.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 5일
(B(:,5) == A(i,1)) is a vector calculation. You cannot combine vector values with other values using && like you do. You will need to switch to & instead of &&
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 1월 5일
It would have been faster if you had included the actual error message, we could have explained hours ago...
Image Analyst
Image Analyst 2016년 1월 5일
Or if he had used my code in his duplicate question instead of unvectorizing the operation.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by