how to find index in matrix and average data ?

2 views (last 30 days)
Lilya on 24 May 2022
Commented: Lilya on 25 May 2022
Hi,
I am working on a matrix with a dimension of (72,45,1090) which I want to find all the data is >= 15.
I used the following lines to extract the indices but want to return the indices back to have a 3d matrix, not a vector array.
Once this happened, I calculated a weekly average of the third dimension (1090), but it returned an error.
Any help will be appreciated.
% extract data greater than 15 degree
ng=find(sst>=15);
sz = size(sst);
[row, col, page] = ind2sub(sz,ng);
l = sst(sub2ind(sz,row,col,page));
% calculte weekly average
wsst = reshape(sst,sz(1),sz(2),[],7);
out = nanmean(wsst,4)

You can try the following, instead of "finding" the locations, which will be the indices, just use the locations like this:
sst = rand(10,20,30)*20;
ng=(sst>=15);
valuesAbove15 =sst(ng);
that would find all the values that are above 15. Notice that I simulated sst with random values of size [10 20 30].
if you then use the find:
ng2=find(sst>=15);
[row, col, page] = ind2sub(sz,ng2);
That would generate vectors of the locations of the same size as valuesAbove15, so you can now combine like this
mean(valuesAbove15(page==30))
and that would calculate the average values of the values above 15, that are in page==30. Just change to your values of interest and that would solve your problem hopefully.
Lilya on 25 May 2022
Thanks very very much!!