I have a for loop of a certain number of days, we'll say 20. I created a logical matrix for points with a value less than or equal to 33. Each salinity point corresponds to a specific lat/long so the position that this value occurs is important. I want to loop through each day and essentially tally which days each point has a value less than 33.
for j = 1:20 if L = logical(salinity <=33)
Example: Let's say I have (3) 2x3 matrices
0 1 1 0 1 0
0 1 0 0 1 1
1 1 1 0 1 0
I want one matrix of the same size at the end of the loop that looks like this:
1 3 2 0 3 1
I can't get accumarray to work because I can't keep the tally of values in the same position in the matrix. If someone could show me how to do that, I think it would work. I'm also concerned about memory. Although I used 20 days as an example, I actually have decades of data to run, so I don't want each logical matrix saved in my workspace.
If it is possible, you could use sum, specifying the dimension, but assuming that is not possible, you could write the loop as:
total_count=zeros(2,3) for j=1:20 %code that loads the salinity map for index j L=salinity <=33; total_count(L)=total_count(L)+1; end
Catherine, I don't think you need a loop at all. If you have some 3D salinity matrix sal, whose dimensions correspond to lat,lon,time or lon,lat,time, you can just use sum, similar to Stephen's suggestion, but here we'll do it all in one go.
Below I'm creating sal as some sample data so you can run the example. It says sal has a mean value of 35 psu, +/- some random noise. Then we count up all the sal values less than or equal to 33:
% 5 years of 1-degree data centered on 35 psu: sal = 35+randn(360,180,365*5);
% Sum of days with low salinity: Lessthan33 = sum(sal<=33,3);
% Plot it up nice: imagesc(-179.5:179.5,-89.5:89.5,Lessthan33) axis xy xlabel 'longitude' ylabel 'latitude' cb = colorbar; ylabel(cb,'number of days when salinity is less than 33 psu')