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]

采纳的回答

Stephen23
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')
T = 7x5 table
GroupID X δ18O WR δ18O WR1 δ18O WR2 ________ ________ ________ ________ ________ {'DMCQ'} 0.06404 0.42084 0.75563 0.28976 {'AAA' } 0.21165 0.93118 0.40206 0.29311 {'TGA' } 0.9315 0.99812 0.16826 0.59712 {'DMCQ'} 0.67944 0.65717 0.55271 0.2492 {'BBB' } 0.49654 0.88852 0.31354 0.81825 {'TGA' } 0.030262 0.46033 0.90109 0.75549 {'DMCQ'} 0.17957 0.050664 0.69379 0.18416
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
  • get rid of all of those superfluous square brackets,
  • use more efficient e-notation rather than powers of ten.
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)
out = 5x5 table
GroupID δ18O WR Diff Beta Temp ________ ________ _______ ____ ____ {'DMCQ'} 0.42084 -4.9534 0.58 250 {'TGA' } 0.99812 -1.8051 0.1 400 {'DMCQ'} 0.65717 -4.7171 0.58 250 {'TGA' } 0.46033 -2.3429 0.1 400 {'DMCQ'} 0.050664 -5.3236 0.58 250
  8 个评论
Stephen23
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.
Erin Summerlin-Donofrio
I will keep trying. Thanks.
I guess I was attempting to couple if statments with the indexing by incorrectly assuming the command was similar to general if then statements. Currently having trouble using addvars but I'll figure it out. Sorry for taking up your time.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by