Elegant way to batch extract all as single data from Data Pool
2 ビュー (過去 30 日間)
古いコメントを表示
I am Matlab beginner, which to know if there's a quick way of indexing/slicing data into individual variable.
dataPool=magic(5)
Naive method is
p=dataPool(1,1);
q=dataPool(1,2);
r=dataPool(1,3);
s=dataPool(1,4);
t=dataPool(1,5);
Can I do a batch processing on this? For instance:
[p,q,r]=dataPool(1,:)
I pretty sure there must be some methods published in Mathworks, but it is hard to describe this problem as search string/keyword to look for help. Let me know if you know one webpage that talk about this. Your help is very much appreciated.
Thank you.
More information:
I am developing an algorithm that produces input matrix which composed of m*n (m runs* n parameter). Matrix example:
___________________________________________________
| Parameter1 Parameter2 Parameter3
____________________________________________________
Run#1 | 3 grams 45 degreeCelcius 3 bar Pressure
Run#2 | 4 grams 30 degreeCelcius 4 bar Pressure
These matrix will be extracted row-by-row to run a chemistry experiment, and the experiment program reads by variables' name (because these variables will be processed individually for instrument controller to start functioning), so each parameter must have a unique tag/variable_name. I am looking for a way, that helps to reduce my line of codes (i.e. Parameter1=dataPool(:,1);Parameter2=dataPool(:,2);) ,because i have long list of parameters
The convenient method is definitely the simplest way out. Just out of curiousity, if anything else works.
2 件のコメント
Stephen23
2022 年 9 月 4 日
" if there's a quick way of indexing/slicing data into individual variable."
Indexing.
"i.e. Parameter1=dataPool(:,1);Parameter2=dataPool(:,2);)"
Numbering variable names (or more generally, forcing meta-data into variable names) is a sign that you are doing something wrong.
"so each parameter must have a unique tag/variable_name."
Variable names should not matter. Much better data design would for example store all of the meta-data and data in one table, making it trivial to access (and allowing you to really simplify and generalize your code).
dpb
2022 年 9 月 4 日
編集済み: dpb
2022 年 9 月 4 日
@Stephen23's comment +inf
Reinforces what I've been preaching...
採用された回答
Walter Roberson
2022 年 9 月 3 日
Expand = @(c)c{:};
[p, q, r, s, t] = Expand(num2cell(dataPool(1,:)));
That is, no built-in way is provided to split numeric vectors or arrays into different outputs. A built-in way is provided to split cell array into outputs, and a built-in way is provided to convert a numeric array to cell, but the allowed syntax does not permit combining those two without ugly hacks or using a helper function.
6 件のコメント
dpb
2022 年 9 月 5 日
編集済み: dpb
2022 年 9 月 5 日
The application of the anonymous function to make the conversion for the purpose is a needed/useful instance that is "the exception that proves the rule" to use the old saw.
The advice against coding with multiple sequentially-named variables or with variables containing metadata in their names is still good for the general case we see when asked the question as your initial posting did without the specific application -- the beginner approach is very often to do just that and then for every one of those variables end up copying the same code line over and over where instead a single vectorized expression could have been written elegantly. Or, the immediate next question raised is how to create those sequentially-named variables dynamically. It is in that context that the advice against doing so was given.
MATLAB is good about not making copies of argument data in functions if the data are passed in as an array and then local variables are created from elements of the array; it will use references to the array addresses instead, so that an alternative I would suggest if the function RunExperiment() is your function and not one you can't modify would be to change its syntax to pass the array and move the code to create the individual variables from the array inside it, out of the higher-level user code.
dpb
2022 年 9 月 6 日
編集済み: dpb
2022 年 9 月 6 日
Stylistic note -- if were really going to go this route, NB that you could make the top level user code a little simpler by defining dataPool as a cell array to begin with -- then could expend with the helper function entirely as all it really gains you is dereferencing the cell array that you've had to create manually in the argument...
dataPool=num2cell(magic(5)); % the array as cell array instead
[p, q, r, s, t]=dataPool{1,:} % split to variables instead...
As an aside, I've always thought this is a case deal should handle (maybe with a user option) differently; if the number of elements of the RHS matches the number of targets, assign 1:1 instead of duplicating as it does...
その他の回答 (1 件)
dpb
2022 年 9 月 3 日
編集済み: dpb
2022 年 9 月 3 日
The real answer in MATLAB is "Don't Do That!" -- making variables out of array elements generally is not the way to write efficient MATLAB code and utilize its primary reason for being -- namely, the vectorized functionality.
This is a hard concept for those who come to MATLAB from other procedural languages, granted, but is key to making effective use of MATLAB.
Use array indexing instead...
Granted, occasionally it is convenient to address array segments as variables for convenience such as in your above example
x=dataPool(:,1);
y=dataPool(:,2:end);
plot(x,y)
lets you write a shorthand for the plot() function or other operations on the independent variable and its corollary dependent ones. Almost all MATLAB builtin function operate by column by default so the above plots the four columns against the first. Taking advantage of this is the key to effective and efficient MATLAB code both from the coding side as well as then performance when running.
What would be the end purpose of creating all these named variables that prompted the Q? Given a use case, we can probably illustrate "the MATLAB way" instead...
ADDENDUM
NOTA BENE: If x,y above are not manipulated numerically, then behind the scenes MATLAB will NOT create physical copies of the data but only references to the original. Hence, there is not a penalty for the coding convenience in this case.
Where the problem arises is that one you have created your above named variables, then you have no recourse going forward to use them but to use those specific names -- any generality is lost except by the arcane and performance-killing use of the not-to-be-mentioned-in-polite-company eval. And, you definitely do NOT want to even think about going down that route.
4 件のコメント
dpb
2022 年 9 月 3 日
"... developing an algorithm that produces input matrix ... to run a chemistry experiment,"
If you have the Statistics Toolbox, it contains a pretty good selection of experimental design tools D(esign)O(f)E(xperiments) or DOE that might possibly be of interest/help in setting up the design matrix.
Includes full/fractional factorials, RMS (response surface model-specific), various D-Optimal designs, etc., etc., ...
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!