How to call all arrays that start with the name "CV_"?
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (2 件)
James Tursa
2019 年 10 月 2 日
Don't do that. See this post for reasons why:
Other methods (e.g., cell arrays) are better for this.
3 件のコメント
Walter Roberson
2019 年 10 月 2 日
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
2019 年 10 月 3 日
編集済み: Stephen23
2019 年 10 月 3 日
" 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
2019 年 10 月 2 日
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 件のコメント
Walter Roberson
2019 年 10 月 2 日
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
参考
カテゴリ
Help Center および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!