Find the elements in matrix that are not within the specified range
2 ビュー (過去 30 日間)
古いコメントを表示
Hello all,
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
iswithin is a utility function of dpb
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);
The program needs to be modified in the following way. I have a matrix 288x55. I need to find the elements in matrix that are not within the specified range. Unlike the question above (program returned true if only one element is not within the specified range), this program should return true only if two elements one after another in a matrix are not within the range.
Note that program needs to return true only if elements are one after another.
The limits are 207 and 253.
For example, if a given matrix is:
232 209 237
241 226 233
237 229 235
205 215 205
227 227 219
227 240 227
242 221 238
245 205 234
217 218 229
218 233 204
232 221 213
230 241 246
232 223 232
217 233 230
214 223 226
229 238 *265*
245 210 *262*
238 250 228
229 227 236
225 228 246
216 213 228
241 230 242
240 229 242
228 215 233
235 237 214
233 *254* 240
241 *255* 231
254 229 249
Then isOK should return 0 1 1
Any help is greatly appreciated.
Please feel free to ask questions if the problem needs some clarification.
Thank you
3 件のコメント
dpb
2017 年 6 月 2 日
"this program should return true only if two elements one after another in a matrix are not within the range."
Two and only two more than one?
Also, what's the definition of "elements one after another in a matrix", precisely? In memory order, column order, row order, does count start over at each column/row...?
回答 (1 件)
Andrei Bobrov
2017 年 6 月 2 日
a = [232 209 237
241 226 233
237 229 235
205 215 205
227 227 219
227 240 227
242 221 238
245 205 234
217 218 229
218 233 204
232 221 213
230 241 246
232 223 232
217 233 230
214 223 226
229 238 265
245 210 262
238 250 228
229 227 236
225 228 246
216 213 228
241 230 242
240 229 242
228 215 233
235 237 214
233 254 240
241 255 231
254 229 249];
n = size(a,2);
t = a < 207 | a > 250;
k = cumsum([false(1,n);diff(t) == 1]).*t;
out = any(histc(k,1:max(k(:)+1)) >= 2);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!