Why do I receive empty outputs in cell arrays after an if statement?

1 回表示 (過去 30 日間)
gsourop
gsourop 2018 年 10 月 25 日
コメント済み: Adam Danz 2018 年 10 月 25 日
Hi everyone,
I am applying an if statement in doubles and allocate outputs in cell arrays, such as
stars{1}='*'; stars{2}='**'; stars{3}='***-';
crvalue{1} = 1.282; crvalue{2} = 1.645; crvalue{3} = 2.326;
for i = 1 : 8
C = randn(17,2);
for j = 1 :size(C,1)
if C(j,2) > crvalue{1}
results{j,i} = sprintf('%.2f%s', C(j,1), stars{1});
if C(j,2) > crvalue{2}
results{j,i} = sprintf('%.2f%s', C(j,1), stars{2});
if C(j,2) > crvalue{3}
results{j,i} = sprintf('%.2f%s', C(j,1), stars{3});
if C(j,2) < crvalue{1}
results{j,i} = C(j,2);
end
end
end
end
end
end
I am only doing one-sided hypothesis testing and want to find an easier way to get my results. However, the cell array 'results' delivers empty cells.

採用された回答

Dennis
Dennis 2018 年 10 月 25 日
編集済み: Dennis 2018 年 10 月 25 日
You nested your if statements, hence the last statement
if C(j,2) < crvalue{1}
will only be executed if the previous statements are true.
stars{1}='*'; stars{2}='**'; stars{3}='***-';
crvalue{1} = 1.282; crvalue{2} = 1.645; crvalue{3} = 2.326;
for i = 1 : 8
C = randn(17,2);
for j = 1 :size(C,1)
if C(j,2) > crvalue{3}
results{j,i} = sprintf('%.2f%s', C(j,1), stars{3});
elseif C(j,2) > crvalue{2}
results{j,i} = sprintf('%.2f%s', C(j,1), stars{2});
elseif C(j,2) > crvalue{1}
results{j,i} = sprintf('%.2f%s', C(j,1), stars{1});
elseif C(j,2) < crvalue{1}
results{j,i} = C(j,2);
end
end
end

その他の回答 (1 件)

Adam Danz
Adam Danz 2018 年 10 月 25 日
編集済み: Adam Danz 2018 年 10 月 25 日
This version is a lot cleaner and fixes some other issues in your code such as allocating the results cell array.
stars{1}='*'; stars{2}='**'; stars{3}='***-';
crvalue = [1.282, 1.645, 2.326];
results = cell(17, 8);
for i = 1 : 8
C = randn(17,2);
for j = 1 :size(C,1)
g = max(find( C(j,2) > crvalue, length(crvalue)));
if isempty(g)
results{j,i} = C(j,2);
else
results{j,i} = sprintf('%.2f%s', C(j,1), stars{g});
end
end
end
results =
17×8 cell array
{[-0.17809]} {[ 0.21487]} {[ 0.1679]} {'2.16*' } {[-0.22357]} {[ -1.2048]} {[-0.076898]} {[ 0.81204]}
{[ -1.2298]} {[ 0.79815]} {'0.81*' } {[ -0.78924]} {[ -1.8436]} {[ -1.2214]} {[ -0.79468]} {[-0.82839]}
{[ 0.23592]} {[ 0.66115]} {[ 0.13897]} {[ 0.28164]} {[ 1.2646]} {[ -0.93917]} {[ -0.86142]} {[-0.96019]}
{[ -1.5158]} {[ 0.80677]} {'-0.19**' } {[ 0.91823]} {[0.052461]} {[ 0.12853]} {[ 0.15585]} {[-0.44851]}
{[ 0.14905]} {[ -1.3359]} {'0.09*' } {[-0.072479]} {[ 0.39338]} {[ 0.22861]} {[ 0.24215]} {[ 0.79749]}
{'-0.05**' } {[ 0.1088]} {[ 0.51407]} {[ -0.4045]} {[-0.68554]} {[ 0.67834]} {[ -0.23146]} {[-0.23779]}
{[ -2.0189]} {[ -1.6722]} {[ 1.1261]} {[ -0.70628]} {[ -1.4358]} {[ -0.56188]} {[ -1.0168]} {[-0.26382]}
{[-0.49984]} {[ 0.67732]} {[-0.61416]} {[ -0.37119]} {[ 0.22429]} {[ 0.022608]} {[ -1.1964]} {[ -0.1657]}
{[ 1.086]} {[-0.12485]} {[-0.58216]} {[ 0.22281]} {[ 0.49732]} {[ -0.42034]} {[ -1.8448]} {[ 0.74226]}
{[ 0.91469]} {'-0.54**' } {[ -1.6322]} {[ 0.67979]} {[-0.51625]} {[ -1.0327]} {[ -0.02224]} {[ 0.43704]}
{[-0.31121]} {[ 0.58413]} {[-0.25497]} {[ -1.2525]} {[-0.59535]} {[ 1.0832]} {[ -0.7387]} {[-0.28457]}
{[ 1.2332]} {[ 0.16294]} {'-0.17**' } {[ 0.65908]} {[ 1.0631]} {[-0.013716]} {[ 0.39654]} {[ 0.4468]}
{[-0.75311]} {'-2.48*' } {[-0.59098]} {[ 1.0899]} {[ 0.22924]} {[ 0.8879]} {[ 1.2352]} {[ 1.1911]}
{[-0.35791]} {[ 0.57181]} {[ 0.11582]} {[0.0011519]} {[ 0.1029]} {[ 0.35997]} {[ -0.52432]} {[ 0.20621]}
{[-0.78979]} {[ 0.26162]} {[ 0.72697]} {[ -0.23341]} {[ 0.35146]} {[ -0.21522]} {[ -1.0894]} {[ 0.76702]}
{[ -1.5112]} {[ -1.7766]} {[-0.74442]} {[ 0.71567]} {'1.25***-'} {[ 0.41003]} {[ 0.58509]} {[ -0.8177]}
{[ -1.7082]} {[ 1.2142]} {[-0.45693]} {[ -0.1592]} {'0.55**' } {[ 0.088993]} {'2.14**' } {[ 0.7668]}
  2 件のコメント
gsourop
gsourop 2018 年 10 月 25 日
Hi there,
Thank for your time to answer my question. When I run the code I get an error 'Undefined operator '>' for input arguments of type 'cell'
The code I run is
stars{1}='*'; stars{2}='**'; stars{3}='***-';
crvalue{1} = 1.282; crvalue{2} = 1.645; crvalue{3} = 2.326;
for i = 1 : 8
C = randn(17,2);
for j = 1 :size(C,1)
g = max(find( C(j,2) > crvalue, length(crvalue)));
if isempty(g)
results{j,i} = C(j,2);
else
results{j,i} = sprintf('%.2f%s', C(j,1), stars{g});
end
end
end
results
Adam Danz
Adam Danz 2018 年 10 月 25 日
Oops, I forgot the first 3 lines of code. I updated my answer to include them.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by