how to find index in matrix and average data ?
2 views (last 30 days)
Lilya on 24 May 2022
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
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)
Constantino Carlos Reyes-Aldasoro on 24 May 2022
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;
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:
[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
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.