Calculate the Standard Deviation based on multiple conditions
9 views (last 30 days)
Show older comments
Hello everyone,
I am hoping to get some help with calculating the standard deviation for my large data set. I have attached just a small snippet of it here.
There are over 4000 rows and 30 columns. It is broken down into some basic information, such as date, month, and season. The data that I would like to calculate the standard deviation is in columns labeled A-I, again just a 1 year snippet. It is in a text file. Tried to attach it, but Chrome didn't like that idea. So, sorry it is just an image.
Here is an example of what I would like to do. I would like to find the standard deviation of all of the values in the A column from Spring, labeled as NV, and dated between 1987-1997. I have been sorting this data into the different groups, but it is quite a bit. There must be a better way. Is there a way to do this with a formula and also skip all of the blank values?
Using MATLAB Online, so be kind with the code.

4 Comments
Stephen23
on 12 Jul 2025
"I tried to upload a copy of the data, but Chrome didn't like it."
Either
- change the file extension to .TXT, or
- zip it into a ZIP file,
then upload it.
Answers (1)
Star Strider
on 9 Jul 2025
Edited: Star Strider
on 9 Jul 2025
It would definitely help to have the data, or at lest a representative sample of it. One option would be to use the function, then attach the .zip file, providing it meets the 5 MB size limit.
That aside, one option would be to do something like this --
Seasons = reshape(repmat(["Spring","Summer","Autumn","Winter"], 4,1), [], 1);
Seasons = repmat(Seasons,4,1);
Years = reshape(repmat([1987,1988,1989,1990], 16, 1), [], 1);
A_col = randn(size(Seasons));
T1 = table(Years,Seasons,A_col);
disp(T1)
[G,ID1,ID2] = findgroups(T1.Years, T1.Seasons=="Spring");
Out = accumarray(G, (1:numel(G)).', [], @(x){[T1.Years(x(1)), T1.Seasons(x(1)), std(A_col(x))]});
LvS = cell2mat(cellfun(@(x)strcmp(x(:,2),"Spring"), Out, Unif=0));
Result = array2table(cell2mat(Out(LvS,:)), VariableNames=["Year","Season","StDev"])
Unfortunately, findgroups is failing me here, and returning both "Spring" and "Summer" values. Creating and using 'LvS' solves that problem.
Make appropriate changes with your data to get the results you want.
To select only 1987-1997, I would just use logical indexing to begin with to create a secondary table with only those years, then use it for your calculations.
EDIT -- (9 Jul 2025 at 13:04)
An easier approach would be to simply identify the "Spring" rows and then call accumarray --
T1Spr = T1(T1.Seasons=="Spring",:)
[Yu,ia,Yidx] = unique(T1Spr.Years,'stable');
Resultc = accumarray(Yidx,(1:numel(Yidx)),[],@(x){[T1Spr.Years(x(1)), std(T1Spr.A_col(x))]});
Result = array2table(cell2mat(Resultc), VariableNames=["Years","std A"])
You can use the same sort of approach to isolate the desired year range as the first step, then isolate the "Spring" entries as I did here. The rest should be straightforward.
.
3 Comments
Star Strider
on 9 Jul 2025
Possibly. There are likely several ways to do this. I chose the most basic approach, because it should be the most generally applicable.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!