Discarding certain values of a variable.

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?
Thank you in advance,
Regards,
Maria
P.D.- I can attach or send the complete file if it is necesary.

2 Comments

@Maria Jose Legaz There are no files attached to this question.
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

Sign in to comment.

Answers (2)

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;
Hs(mask) = 0;

4 Comments

Thank you so much, Walter.
It is work for Hs. The matrix of Hs put zero in the range Hs<0.2 or Hs>0.4.
The problem is that final plot of the program it is not represeting well the values.
The program plot colour in points that Hs is zero.
This is another issue. I try to think about it.
Anyway, if someone knows why this is happening, please tell me.
Thank you again
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;
Hs(mask) = 0;
zero_locs = find(mask);
plot(Hs, '-*', 'MarkerIndices', zero_locs, 'MarkerEdgeColor', 'r')
The red * will appear only at the 0 locations.
Thank you so much for your answer, Walter.
But the program is longer than the lines I have written in the email. I attached the files previously. The main program is the file called Modification.
When I run the complete program the result is not correct. The plot is not correct.
If you or someone can help me, I would be more than grateful.
Thank you again, Walter.
Best regards,
María
Hi!
Now, the system allows me to attache the files.
Thank you,
Maria

Sign in to comment.

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)
Hs(mask) = 0; % Make elements in the mask locations zero.
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 Comments

Hello Imagen Analyst,
I am so sorry for not explaining well.
Now, I am trying to do my best.
I attach the file called "original" this is the program.
When I run, It appears this plot.
Also, I attach the file called " Walter_modification".
When I run the file called "Walter_modification", appears this plot.
There is colour in points with Hs values of 0,1; 0,15... .It is supposed these points should be white.
However, if you see the values of Hs as a result of a run in "Walter modification", I attach the image,
It does not appear values less than 0.2 and bigger than 0.4 what it is corret.
The idea of the mofication is that the program does not calculate the energy values for Hs<0.2 or Hs>0.4. So I thought assign value 0 to the Hs in this range.
Sorry again for not explain better previously.
If you have any question, please tell me.
Thank you so much for your help.
Best regards,
María,
P.D.- I have try attach the file, but the web said to me the next.
Sorry again
Hello!!
The problem it is solved. I have to removed the elements as Walter said.
Thank you so much!!
Maria.
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. 🙂

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 23 Sep 2022

Commented:

on 25 Sep 2022

Community Treasure Hunt

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

Start Hunting!