How to implement the counter (really simple)?

1 回表示 (過去 30 日間)
Rengin
Rengin 2015 年 9 月 23 日
コメント済み: C.J. Harris 2015 年 9 月 23 日
% Dear Users,
% My problem and simple script are as below:
clear all
clc
nrow=5;
ncolumn=3;
maxinfeas=1;
Infeasibles=zeros(nrow,1);
A=[-1 5 6 ; 4 2 3 ; 9 -7 1 ; 7 3 8 ; 4 0 1];
B=[75 60 90 ; 80 150 65 ; 20 10 40 ; 70 30 80 ; 100 20 35];
for nr=1:nrow
for nc=1:ncolumn
if A(nr,nc)>0 && B(nr,nc)<=100
Infeasibles(nr)=0;
else
Infeasibles(nr)=maxinfeas;
end
end
end
% Although expected matrix is Infeasibles=[1;1;1;0;1], Matlab adjusts it as Infeasibles=[0;0;0;0;0]
% How can I correct the code?
% Thanks a lot!

回答 (2 件)

C.J. Harris
C.J. Harris 2015 年 9 月 23 日
編集済み: C.J. Harris 2015 年 9 月 23 日
Infeasibles = any((A<=0) | (B>100),2);
  2 件のコメント
Rengin
Rengin 2015 年 9 月 23 日
Isn't it possible to put it into a for loop? It is more convenient for my whole script.
C.J. Harris
C.J. Harris 2015 年 9 月 23 日
Sure, just update your original code, changing the line:
Infeasibles(nr)=0;
To:
Infeasibles(nr)=max(0,Infeasibles(nr));

サインインしてコメントする。


Thorsten
Thorsten 2015 年 9 月 23 日
You can write
idx = ~(all(A > 0, 2) & all(B < 100, 2))
  1 件のコメント
Rengin
Rengin 2015 年 9 月 23 日
Isn't it possible to put it into a for loop. It is more convenient for my whole script.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by