problems in a for loop

4 visualizzazioni (ultimi 30 giorni)
Yu Zhang
Yu Zhang il 27 Ott 2017
Commentato: Yu Zhang il 7 Nov 2017
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...

Risposta accettata

Walter Roberson
Walter Roberson il 27 Ott 2017
Generalized to permit multiple years:
B = A( ismember(floor(data(:,1) ./ 10^8), year), :)
  11 Commenti
Walter Roberson
Walter Roberson il 7 Nov 2017
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 il 7 Nov 2017
thanks! i forgot to check other arrays.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by