How to process multiple matrices. Checking whether the elements of a matrix are within the specified range
1 view (last 30 days)
Show older comments
Hello. I have 55 matrices 288x1 named Load1, Load2, Load3, ..., Load55. I need to create a for loop which checks whether the values are between 207 and 253. If one of the values exceeds 253 or is less than 207, I need to display the name of this variable. For example, one of the elements of matrix Load7 is 205. In this case, the program should display 'Load7'. Ultimately, I need to get a list of variables which values are not within specified range. Also, the script has to calculate the total number of variables which values are not within the range.
The output of the script shall look like, for example:
The Loads which are not within the range: Load7, Load25, Load37. The total number of loads: 3
Any help is greatly appreciated. Thank you.
3 Comments
John BG
on 25 Feb 2017
may be Yesbol is new in MATLAB and doesn't know how to LOAD THE DATA INTO 2D ARRAY
may be Yesbol knows a way, but would like the experts to show whether there's a better way.
as trivial as it is, would you please supply a valid answer? if you don't I will
regards
Accepted Answer
dpb
on 24 Feb 2017
Edited: dpb
on 25 Feb 2017
Given you heed the advice in the comment so the data are in array loads
lo=207; hi=253; % set the limits desired
ix=iswithin(loads,lo,hi); % check all are ok, ix is logical array of locations
isOK=all(ix); % if T, all are ok for each column
if all(isOK)
fprintf('All is well\n')
else
bad=sum(~isOK); % number failed columns (loads)
fmt=['Load %2d' repmat(', %2d',1,bad-1) '\n'] % format string to print offending load(s)
fprintf(fmt,find(~isOK)) % and print the message
fprintf('Total number of loads failed: %d\n',bad) % and the total
end
CAUTION: Air code, untested, but should be close.
iswithin is a utility function of mine--
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
ADDENDUM
Dawned on me not that hard to build some data to test...
>> loads=round(randn(20,3)*12+230)
oads =
234 237 224
208 229 226
242 238 217
259 233 224
242 236 228
226 212 231
235 218 229
218 225 237
253 231 231
241 244 252
239 227 234
219 245 252
234 236 221
223 244 236
226 232 227
223 222 237
218 212 237
219 232 204
227 240 214
210 226 213
>> ix=iswithin(loads,lo,hi);
>> isOK=all(ix)
isOK =
0 1 0
>> bad=sum(~isOK);
>> fmt=['Load %2d' repmat(', %2d',1,bad-1) '\n'];
>> fprintf(fmt,find(isOK==false))
Load 1, 3
>> fprintf('Total number of loads failed: %d\n',bad)
Total number of loads failed: 2
>>
Looks OK to me...
4 Comments
dpb
on 25 Feb 2017
Edited: dpb
on 25 Feb 2017
I said it was air code! :)
It's complaining that I put the string to dup last instead of first in the argument list.
That syntax error aside, bad is the sum of failures for each column; figured that would be of interest when started but then noticed you were only reporting the total number of columns failed and didn't change variables but didn't update bad, either...
The quick fix is
bad=sum(~isOK);
instead; isOK is the check for columns. This overwrites the total number in each column if you do later want that additional detail, create another variable for it.
Also note it's the isOK array to print as well instead and the find argument is modified to search the whole array; otherwise as wrote initially the indices are only for those failed columns instead of the locations in the larger array.
I'll edit the Answer as well...but still since you didn't provide any data and I didn't want to take time to try to generate some phantom data, it is untested so debugging is partly your job... :)
More Answers (1)
John D'Errico
on 24 Feb 2017
And this is only one of the reasons why what you did is a terribly bad idea. Combine all into one matrix. Do it once. Get it over with. And next time, learn not to create multiple numbered arrays.
Instead, learn to create matrices. With a matrix, your question becomes one simple line of code to test them all at once.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!