How to call all arrays that start with the name "CV_"?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to check a value in all arrays that their names start with "CV_". File names are like CV_er, CV_wer, CV_jkl and so on. Each of these are arrays that have multiple rows and column. I am interested to find if any member of these arrays has the number larger than 1 in them. I need to report the number and the index (whihc array).
Any idea?
0 Commenti
Risposte (2)
James Tursa
il 2 Ott 2019
Don't do that. See this post for reasons why:
Other methods (e.g., cell arrays) are better for this.
3 Commenti
Walter Roberson
il 2 Ott 2019
No, you would not have that question: you would have to question of how to build all of those into the same variable instead of using separate variables.
Stephen23
il 3 Ott 2019
Modificato: Stephen23
il 3 Ott 2019
" Even if i work with cell arrays I would have the same question!"
Not really: accessing the contents of one cell array is simple and efficient (e.g. using indexing or functions like cell2mat, etc.), unlike what you are trying to do.
'How those starting with "CV_"'
That is exactly what you should avoid doing. Accessing variable names dynamically is one way that beginners force themselves into writing slow, complex, obfuscated, buggy, hard-to-debug code.
James Tursa already gave you one much simpler and more efficient alternative (cell arrays).
Walter Roberson
il 2 Ott 2019
CV_vars = who('CV_*');
vars_gt_1 = {};
for K = 1 : length(CV_vars)
if any( reshape(eval(CV_vars{K}), [], 1)>1 )
vars_gt_1{end+1} = CV_vars{K};
end
end
This is not recommended. You will run into problems using this kind of code, and the problems will be hard to debug, and the code will mysteriously be slow.
2 Commenti
Walter Roberson
il 2 Ott 2019
CV_vars is a cell array of character vectors. CV_vars{K} is one particular character vector. eval() of the character vector returns the content of the variable named by the character vector.
We want to test if there is at least one entry in the variable that is > 1. We cannot assume that the variable is a scalar or vector or even that it is 2D. If it were scalar or vector we could use any(VALUE>1) and if it were scalar or vector or 2D we could use any(any(VALUE>1)), but since we do not know how many dimensions it is, we either have to test the number of dimensions and have a separate test for each, or else we have to reshape the results from however many dimensions it is into a vector so that we can apply just a single any() to it.
An alternate coding would be:
CV_vars = who('CV_*');
vars_gt_1 = {};
for K = 1 : length(CV_vars)
this_var = eval(CV_vars{K});
if nnz( this_var > 1 ) > 0
vars_gt_1{end+1} = CV_vars{K};
... and do something here to report the rows and columns
end
end
A third coding would be:
CV_vars = who('CV_*');
vars_gt_1 = {};
for K = 1 : length(CV_vars)
this_var = eval(CV_vars{K});
loc_linear_idx = find(this_var>1);
if ~isempty(loc_linear_idx)
vars_gt_1{end+1} = CV_vars{K};
... and do something here to report the rows and columns given the linear indices
end
end
Vedere anche
Categorie
Scopri di più su Performance and Memory in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!