# Discarding certain values of a variable.

2 views (last 30 days)
Maria Jose Legaz on 23 Sep 2022
Commented: Image Analyst on 25 Sep 2022
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 CommentsShowHide 1 older comment
Maria Jose Legaz on 23 Sep 2022
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

Walter Roberson on 23 Sep 2022
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;
Maria Jose Legaz on 24 Sep 2022
Moved: Image Analyst on 24 Sep 2022
Hi!
Now, the system allows me to attache the files.
Thank you,
Maria

Image Analyst on 24 Sep 2022
Edited: Image Analyst on 24 Sep 2022
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.
Image Analyst on 25 Sep 2022
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. 🙂

### Categories

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!