- get rid of all of those superfluous square brackets,
- use more efficient e-notation rather than powers of ten.
Calculations involving indexed variables and creating data tables
2 次查看(过去 30 天)
显示 更早的评论
I'm running calculations from a spreadsheet and I have some variables indexed. I want to subtract calculated variables from the indexed variables from my spreadsheet. I need to use values that are dependent on samplele type, for example for the DMCQ samples, I need to use a Beta of 0.58, and for the TGA samples a Beta of 0.1. The way I have the indexing currently set up puts the DMCQ and TGA data into their own columns. This has been fine for making plots but I need to create a final data Excel table of my results.
Is there a way to keep the indexing but have the output values maintain the same order (the order of the Sample ID or the Group ID column) so I can more easily create the table.
Example:
Take all the DMCQ samples from column F and subtract BDPlg_w1 @ 250 degrees Beta=0.58, take the TGA samples from column F and subtract BDPlg_w1 @ 250 degrees Beta=0.2, and make the result one new column beside F in the final table.
Thank you!
%Code from spreadsheet
clear variables
T=readtable('ESDMatlabAsk021125.xlsx',VariableNamingRule='preserve')
%MCQ Min Sep Oxygen Fluids in EQ
C=[250:150:400]
K=[C+273.15]
Temp=K
Beta=[0.58,0.1]
%Fractionation Equations
BDplg_w1 = (2.91-0.76.*Beta(:,1)) .* (10^6./Temp.^2) - 3.41 - 0.41.*Beta(:,1); % Fractionation plag-water OT1967
BDpx_w1=-1.27;%Mathewsetal1983a - Three isotope exchange method. -1.27@600C, -1.08@700C, -0.98@800C
BDpx_w2=3.08.* (10^6./Temp.^2)-5.13.*(10^3./Temp) %Vho et al 2019
BDep_w=3.54.* (10^6./Temp.^2)-5.72.*(10^3./Temp) %Vho et al 2019
BDq_w1=2.51 .* (10^6./Temp.^2)-1.96 %Clayton et al 1972
BDbt_w=3.21.* (10^6./Temp.^2)-6.05.*(10^3./Temp) %Vho et al 2019
BDhbl_w=3.21.* (10^6./Temp.^2)-5.76.*(10^3./Temp) %Vho et al 2019
BDch_w=1.56.* (10^6./Temp.^2)-4.70 %Wenner and Taylor 1971
%Set up indexing
idx_DMCQ = strcmp(T.GroupID,'DMCQ');
TDMCQ = T(idx_DMCQ,:);
idx_TGA = strcmp(T.GroupID,'TGA');
TTGA = T(idx_TGA,:);
%Indexing for each column
%Whole Rock Mill Creek Quarry
WR1_DMCQ=[TDMCQ.("δ18O WR")];
WR2_DMCQ=[TDMCQ.("δ18O WRr1")];
WR3_DMCQ=[TDMCQ.("δ18O WRr2")];
WR1_TGA=[TTGA.("δ18O WR")];
WR2_TGA=[TTGA.("δ18O WRr1")];
WR3_TGA=[TTGA.("δ18O WRr2")];
FEQ_WR1_DMCQ=[WR1_DMCQ(:,:)-BDplg_w1]
0 个评论
采纳的回答
Stephen23
2025-2-12
编辑:Stephen23
2025-2-12
Lets first create some fake data (because you did not upload any sample data):
T = array2table(rand(7,4),'VariableNames',{'X','δ18O WR','δ18O WR1','δ18O WR2'});
T = addvars(T,{'DMCQ';'AAA';'TGA';'DMCQ';'BBB';'TGA';'DMCQ'}, 'Before',1, 'NewVariableNames','GroupID')
We will try to merge your separate processing. The most important change is to avoid having lots of separate variables with meta-data in their names. That is not an approach conducive to using loops and generalising your data processing.
While we are here, lets also
I did not understand all of your requirements, but hopefully this will get you started.
ID = ["DMCQ","TGA"];
Beta = [ 0.58, 0.1];
C = [ 250, 400];
K = C+273.15;
% Fractionation Equations
tmp = (2.91-0.76.*Beta).*(1e6./K.^2) - 3.41 - 0.41.*Beta; % Fractionation plag-water OT1967
% Subtable:
idx = matches(T.GroupID,ID);
out = T(idx,["GroupID","δ18O WR"]);
for ii = 1:numel(ID)
ix = matches(out.GroupID,ID(ii));
wr = out{ix,"δ18O WR"} - tmp(ii);
out{ix,"Diff"} = wr;
out{ix,"Beta"} = Beta(ii);
out{ix,"Temp"} = C(ii);
end
% Show the resulting table:
display(out)
8 个评论
Stephen23
2025-2-14
Do not write IF T.GroupID == "DMCQ". T.GroupID is a vector with many values, so what do you expect IF to do when you provide it with a logical vector of values e.g. [true;false;false;true;...] ?
You already wrote the IDs in one string vector. Now do exactly the same for the temperature and beta values: do not write them out as lots of individual scalar values in lots of separate IF/ELSE or SWITCH statements (unless you really love writing lots and lots of very verbose code). Use vectors and matrices!
Add a nested loop, iterate over the Beta etc. vectors (of the same lengths!), perform your operations, allocate the data back into the table using ADDVARS.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!