フィルターのクリア

If loop using cell arrays

7 ビュー (過去 30 日間)
Bas
Bas 2015 年 9 月 14 日
コメント済み: Titus Edelhofer 2015 年 9 月 14 日
Hi,
I have 2 matrices:
A with 24 arrays of each different length (i.e. A{1} 2362x1, A{2} 2341x1) defining salaries
B with 24 arrays of each different length (i.e. A{1} 2362x1, A{2} 2341x1) contains either a 0 or a 1
And last I have 2 boundary values C= 2600 and D= 1700.
I want to check the following:
if B(each value) == 0 && A(each value) > C
A(this value)= C
elseif B(each value) == 1 && A(each value) > D
A(this value)= D
end
Can anyone help me out?
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 9 月 14 日
There is no such thing as an "if loop". There are "for loop" and there are "while loop" and there are "if statement". A loop would have to execute its body multiple times, but "if" executes the body only 1 time or 0 times.

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

採用された回答

Guillaume
Guillaume 2015 年 9 月 14 日
You don't need any if. Just use logical operations. Your first condition is to replace elements when B is false (i.e. B==0 or simply ~B) and A greater than C, so is simply writen as A(~B & A>C). Similarly your second condition is simply A(B==1 & A>D) (which simplifies to A(B & A>D). You just have to apply that to each cell of the cell arrays:
for cellidx = 1:numel(A)
a = A{cellidx};
b = B{cellidx};
a(~b & a>C) = C;
a(b & a>D) = D;
A{celldix} = a;
end
  1 件のコメント
Bas
Bas 2015 年 9 月 14 日
Thanks Guillaume, this is what I was searching for!

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

その他の回答 (1 件)

Titus Edelhofer
Titus Edelhofer 2015 年 9 月 14 日
Hi,
I'm not 100% sure I understand what you want to do, but I guess it should be like this
for i=1:length(A)
if all(B{i}(:)==0 & A{i}(:)>C)
A{i}(:) = C;
elseif all(B{i}(:)==1 & A{i}(:)>D)
A{i}(:) = D;
end
end
Titus
  4 件のコメント
Guillaume
Guillaume 2015 年 9 月 14 日
編集済み: Guillaume 2015 年 9 月 14 日
Both codes have the following preconditions:
assert(isequal(numel(A), numel(B)));
assert(all(cellfun(@(a,b) isequal(size(a), size(b)), A, B)));
Titus Edelhofer
Titus Edelhofer 2015 年 9 月 14 日
I wish I could vote for a comment! Nice assert calls.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by