Info

This question is closed. Reopen it to edit or answer.

How to create a loop for a matrix?

1 view (last 30 days)
usha d
usha d on 30 Sep 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello I have a November month daily data for 10 stations, matrix of dimension (630*10) from year 1995 to 2015 i.e 1 to 30 rows for November year 95 than next 30 rows for year 96 and so on. I want to find 95th percentile for each year and for each station so that my new matrix dimension(21*10). I want to create a loop for this such that loop will pick up first 30 values and find the percentile than next 30 values and so on.

Answers (1)

Jan
Jan on 30 Sep 2016
Edited: Jan on 30 Sep 2016
result = zeros(21, 10);
cursor = 0;
for k = 1:30:630
startIndex = k;
endIndex = k + 30;
cursor = cursor + 1;
result(cursor, :) = YourCalculations(data(startIndex:endIndex, :));
end
Or in a convenient Matlabish way:
siz = size(data);
data = reshape(data, [30, siz(1) / 30, siz(2)]);
result = prctile(data, 95, 1);
  1 Comment
usha d
usha d on 30 Sep 2016
thank u for answer my ques but i am getting an error "Index exceeds matrix dimension" line: result(cursor, :) = prctile(data(startIndex:endIndex, :),95,1) and my last 21st row contain all zeros which is wrong

Tags

Community Treasure Hunt

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

Start Hunting!