# Discarding certain values of a variable.

2 ビュー (過去 30 日間)
Maria Jose Legaz 2022 年 9 月 23 日
コメント済み: Image Analyst 2022 年 9 月 25 日
Discarding certain values of a variable.
Hello,
I have to do a modification to this program.
Hs=[];
Te=[];
for i=1:ntiempos
spec2=0.25*(spec(1:end-1,1:end-1,i)+spec(2:end,2:end,i)+spec(1:end-1,2:end,i)+...
spec(2:end,1:end-1,i));
s=spec2.*teta2.*frec2;
Hs = [Hs 4*sqrt(sum(sum(s)))];
Te=[Te sum(sum(frec1.*s))/sum(sum(s))]
end
% Calcula el J
J = cff * Te .* Hs.^2;
(This is only a part of the complete program)
I need that the program does not take into account the values of the variable Hs menores de 0.2 o mayores de 0.5.
I have done a modification.
Hs=[];
Te=[];
for i=1:ntiempos
spec2=0.25*(spec(1:end-1,1:end-1,i)+spec(2:end,2:end,i)+spec(1:end-1,2:end,i)+...
spec(2:end,1:end-1,i));
s=spec2.*teta2.*frec2;
Hs = [Hs 4*sqrt(sum(sum(s)))];
Te=[Te sum(sum(frec1.*s))/sum(sum(s))];
if (Hs<0.2) | (Hs>0.5)
Hs=0;
else
Hs = [Hs 4*sqrt(sum(sum(s)))];
end
end
% Calcula el J
J = cff * Te .* Hs.^2;
The modification does not work.
Matlab gives this error:
Arrays have incompatible sizes for this operation.
Error in Modification (line 80)
J = cff * Te .* Hs.^2;
Could someone help me with that, please?
Regards,
Maria
P.D.- I can attach or send the complete file if it is necesary.
##### 2 件のコメント表示非表示 1 件の古いコメント
Maria Jose Legaz 2022 年 9 月 23 日
Hi Steven,
Sorry but I do not know how attach a file.
I attach the file called modification. It is the main file and call the other two files.
Thank you very much in advance,
Best regards,
Maria

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

### 回答 (2 件)

Walter Roberson 2022 年 9 月 23 日
if (Hs<0.2) | (Hs>0.5)
Hs=0;
else
Hs = [Hs 4*sqrt(sum(sum(s)))];
end
In the first condition you replace all of Hs with a scalar 0. In the second condition, you append a value to Hs. The final size of Hs is going to depend on how many times in a row the second condition held at the end of the loop.
I would suggest to you that you do not want to replace all of Hs with 0, and instead want to append a 0.
But... remember that your Hs might be a vector at that point, from accumulated values, so the Hs<0.2 test is comparing all values in the vector, giving as logical vector result. "if" will consider the logical vector to be "true" only if all of the entries in the vector are non-zero
Hs = [Hs 4*sqrt(sum(sum(s)))];
You have that statement before the if as well... so if the condition is satisfied you would effectively have appended the sum twice
I would suggest to you:
Te = zeros(1,ntiempos);
Hs = zeros(1,ntiempos);
for i=1:ntiempos
spec2=0.25*(spec(1:end-1,1:end-1,i)+spec(2:end,2:end,i)+spec(1:end-1,2:end,i)+...
spec(2:end,1:end-1,i));
s=spec2.*teta2.*frec2;
Hs(i) = 4*sqrt(sum(sum(s)));
Te(i) = sum(sum(frec1.*s))/sum(sum(s));
end
mask = Hs < 0.2 | Hs > 0.4;
##### 4 件のコメント表示非表示 3 件の古いコメント
Maria Jose Legaz 2022 年 9 月 24 日
Moved: Image Analyst 2022 年 9 月 24 日
Hi!
Now, the system allows me to attache the files.
Thank you,
Maria

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

Image Analyst 2022 年 9 月 24 日

I'm not exactly sure what this means "the program does not take into account the values of the variable Hs". What does "take into account" actually mean? After the Hs vector is created do you want to make the elements where Hs < 0.2 or more than 0.4 equal to zero, like Walter showed you:
mask = (Hs < 0.2) | (Hs > 0.4)
or do you want to remove those elements like
mask = (Hs < 0.2) | (Hs > 0.4)
Hs(mask) = []; % Remove elements, making vector shorter.
?? Exactly what does "the result is not correct" mean? Why is it not correct? Is the Hs vector wrong, or something else is wrong? Please explain in a lot more detail so this doesn't drag on over days.
##### 3 件のコメント表示非表示 2 件の古いコメント
Image Analyst 2022 年 9 月 25 日
That was me that said to "remove" them. Walter showed you how to set them to 0 like you asked because you originally thought you needed that.
Anyway glad it's working. Perhaps you could "Vote" for my answer since it's what you said you ended up using. 🙂

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

### カテゴリ

Find more on Logical in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by