Discarding certain values of a variable.

조회 수: 2 (최근 30일)
Maria Jose Legaz
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?
Thank you in advance,
Regards,
Maria
P.D.- I can attach or send the complete file if it is necesary.
  댓글 수: 2
Steven Lord
Steven Lord 2022년 9월 23일
@Maria Jose Legaz There are no files attached to this question.
Maria Jose Legaz
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
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;
Hs(mask) = 0;
  댓글 수: 4
Maria Jose Legaz
Maria Jose Legaz 2022년 9월 24일
이동: Image Analyst 2022년 9월 24일
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
Maria Jose Legaz
Maria Jose Legaz 2022년 9월 24일
이동: Image Analyst 2022년 9월 24일
Hi!
Now, the system allows me to attache the files.
Thank you,
Maria

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2022년 9월 24일
편집: 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)
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
Maria Jose Legaz
Maria Jose Legaz 2022년 9월 25일
Hello!!
The problem it is solved. I have to removed the elements as Walter said.
Thank you so much!!
Maria.
Image Analyst
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. 🙂

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by