if statement with multiple conditions within cells

1 Ansicht (letzte 30 Tage)
Paola Castiblanco
Paola Castiblanco am 16 Dez. 2019
Kommentiert: Adam Danz am 17 Dez. 2019
Hello ,
I have two 75x1 cells called goodcase and badcase , each cell is composed by 75 logical vectors of the same size (241200 elements).
I want to find if the following case exists between the 2 cells:
one of the first 1200 elements of the logical vector in badcase is equal to 1 ,(let's call this element u) and one of the elements placed in the position u+1*1200 or the position u+2*1200 or u+3*1200 or u+4*1200 and so on until the highlighted numbers equal to 200 in the logical vector in goodcase is also equal to 1
I want to know the values of u and the values of the highlighted numbers that accomplish this case.
I tried with this code but It does not give me any result even when I have checked "by hand" that the case exists.
cases3=cell(75,1);%preallocating size of the statement
for gcellid=1:numel(goodcase)
cases3{gcellid}=zeros(1200,201);
end
for gcellid=1:numel(goodcase);
ii=1:numel(1200);
for y=1:200;
if badcase{gcellid}(ii)==1 & goodcase{gcellid}(ii+y*1200)==1;
cases3{gcellid}(ii,y)=1
end
end
end
PP3=cellfun(@(x) find(x(:,:)==1),cases3,'un',false)%linear position of the cases I want

Akzeptierte Antwort

Adam Danz
Adam Danz am 16 Dez. 2019
Bearbeitet: Adam Danz am 16 Dez. 2019
Some of your question required interpretation. See the inline comments below and follow along line-by-line to understand how I interpretted the problem. If a line does not match your expectations, indicate the line and clarify the interpretation.
The final results are put into a table named results.
% Recreate inputs based on the following description:
% I have two 75x1 cells called goodcase and badcase ,
% each cell is composed by 75 logical vectors of the
% same size (241200 elements).
rng(14159) % for reproducibility of results for this demo ONLY
goodcase = arrayfun(@(i){rand(1,241200)<0.05},1:75)';
badcase = arrayfun(@(i){rand(1,241200)<0.0001},1:75)';
% I want to find if the following case exists between the 2 cells:
% one of the first 1200 elements of the logical vector in badcase
% is equal to 1 ,(let's call this element u)
% [My interpretation is that u is a row or column number of the first TRUE in badcase{n}(1:1200)]
u = cellfun(@(x)find(x(1:1200),1,'first'),badcase,'UniformOutput',false);
% and one of the elements
% placed in the position u+1*1200 or the position u+2*1200 or u+3*1200
% or u+4*1200 and so on until the highlighted numbers equal to 200 in
% the logical vector in goodcase is also equal to 1
% [My interpretation is that you're searching in goodcase for these values]
notIsEmpty = ~cellfun(@isempty,u);
idx = cell(size(u));
idx(notIsEmpty) = cellfun(@(x,i)find(x(i+(1:200)*1200)),goodcase(notIsEmpty),u(notIsEmpty),'UniformOutput',false);
% I want to know the values of u and the values of the highlighted
% numbers that accomplish this case.
% [I'll organize them into a table]
results = table(u,idx,'VariableNames',{'u','idx'},'RowNames',cellstr(string(1:numel(u))));
% Let's look at he first few rows of results
% Empty values indicate a TRUE was not found in the first 1:1200 values of badcase{n}
head(results)
% ans =
% 8×2 table
% u idx
% ____________ _____________
%
% 1 {1×0 double} {0×0 double}
% 2 {1×0 double} {0×0 double}
% 3 {[ 609]} {1×10 double}
% 4 {1×0 double} {0×0 double}
% 5 {[ 801]} {1×11 double}
% 6 {1×0 double} {0×0 double}
% 7 {1×0 double} {0×0 double}
% 8 {1×0 double} {0×0 double}
% We see that element #3 of badcase contains a TRUE in element #609.
% To see the results for goodcase, results.idx{3}
% Element #3 in goodcase contains TRUE values for u = [3 40 49 53 67 74 85 104 179 190]
% return random number generation
rng shuffle
  2 Kommentare
Paola Castiblanco
Paola Castiblanco am 17 Dez. 2019
This is what I was looking for.Thank you !
Adam Danz
Adam Danz am 17 Dez. 2019
Glad I could help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by