problems in a for loop

조회 수: 4 (최근 30일)
Yu Zhang
Yu Zhang 2017년 10월 27일
댓글: Yu Zhang 2017년 11월 7일
Hi everyone, i have a question about a for loop. i got a 2-column data, the first column is date data(e.g. 194501011000 means year, month, data and time 10:00), the second column is rainfall depth. What i want to do is select all the data that happend in the same year (like 1945) and then output a new table that only contains rainfall depth for year 1945. The code is as below:
function [B]=Seperate_DATA(data,year)
a=data(:,3)
b=num2str(a)
for i=1:size(data)
if b(i,1:4)==num2str(year)
A(i)=data(i,4)
end
B=reshape(A,[],1)
end
end
Firstly i transfered the time column to string and then selected the first four characters so that it can be compared to the if conditions. data(i,4) means the orignal data that contains the rainfall depth. It works when i put the year 1945, but when i put 1946, it will give me the 1945 results and 1946 results together, with data in 1945 all 0 values. Is there anyone who can tell me where is the problem is? Thank you. I am just a beginer...

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 27일
Generalized to permit multiple years:
B = A( ismember(floor(data(:,1) ./ 10^8), year), :)
  댓글 수: 11
Walter Roberson
Walter Roberson 2017년 11월 7일
The totals are already in the cell array max6val.
If you want the start dates rather than the dates for all 6 items, then
shortinfo = cellfun(@(M, tot6, idx6) [M(idx6,1), tot6], E, max6val, max6idx, 'Uniform', 0);
This has the start date in the first column and the 6 day total in the second column.
Yu Zhang
Yu Zhang 2017년 11월 7일
thanks! i forgot to check other arrays.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by