A is less than a threshold

m=256;
e=0.5;
for x=1:m
for y=1:m
if A(x,y)<0.5
s_dark(x,y)=A(x,y)^e;
end
end
end
Hello everyone.I have some problem in this code.The values less than 0.5 shold be stored in a variable s_dark.But i didn't get it.It store values less than 0.5 and also greater than 0.5.I've attached the s_dark variable below.please see through it.your help is really appreciated.

 Respuesta aceptada

Mehmed Saad
Mehmed Saad el 15 de Abr. de 2020
Editada: Mehmed Saad el 15 de Abr. de 2020

0 votos

Because in your code
s_dark(x,y)=A(x,y)^e;
suppose A(x,y) = 0.47;
where e = 0.5;
0.47^0.5
ans =
0.6856
That is why you are getting values greater than 0.5
i think what you are trying to do is
m=256;
e=0.5;
A = A.^e;
for x=1:m
for y=1:m
if A(x,y)<0.5
s_dark(x,y)=A(x,y);
end
end
end
i am assuming that s_dark is initialized in the code earlier

9 comentarios

Rocky
Rocky el 15 de Abr. de 2020
Thank you for your quick response.I have one doubt,when i ran the code the last value of s_dark has 1.1866..can you tell me why it is coming like this.I'm attaching the variable value for your reference..
Mehmed Saad
Mehmed Saad el 15 de Abr. de 2020
two possibilities
  1. Wrong initialization
  2. Negative values i.e. -1.4080 maybe as it is still less than 0.5
Rocky
Rocky el 15 de Abr. de 2020
Okay.Thank you for your help
Image Analyst
Image Analyst el 15 de Abr. de 2020
What value do you think it should be? What is A(256, 256)?
Why are you using this for loop instead of my vectorized code like most MATLABers would use?
Rocky
Rocky el 15 de Abr. de 2020
I need to check each and every values is less than 0.5 or not.for that reason I'm using for loop..A(x,y) where x and y are rows and columns of a matrix..Here I've taken image size as 256×256..so I put 1:256.
Image Analyst
Image Analyst el 15 de Abr. de 2020
I guess you haven't yet encountered the part of MATLAB where you learned how to use vectorized array operations. Too bad, though I thought they covered that really early on. So that whole set of nested for loops and if statement (7 lines of code) is done simply by these two lines of code:
map = A < 0.5; % Logical matrix of where A is less than 0.5.
s_dark(map) = A(map) .^ e;
You'll need to learn this easy concept if you want to use MATLAB much.
Anyway, you didn't answer the question of what you expected the bottom right element of A to be. What did you expect for A(256, 256) instead of what you showed?
Stephen23
Stephen23 el 16 de Abr. de 2020
Matrix and array operations are introduced in the second part of the basic tutorials:
Rocky
Rocky el 16 de Abr. de 2020
Thank you for providing the link I will definitely go through it @stephen codeldick
Rocky
Rocky el 16 de Abr. de 2020
@Image Analyst..In my above code I simply comparing the A matrix with a threshold..if it is less than that threshold the values are stored in dark variable.And I raising e to the power of dark one..This is what I am doing.while using for loop instead of 0:255 I'm taking 1:256..I think I have clarified your question.If I'm wrong with my assumption you can point out my mistakes..

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 15 de Abr. de 2020

1 voto

Not exactly sure what you want where A > 0.5, a value of 0 or of the original A? So here are both options:
% Initialization steps.
m = 256;
A = 0.7 * rand(m, m)
e = 0.5;
% OPTION 1
% s_dark equals A except where A is less than 0.5 where
% it will replace values of A less than 0.5 with
% the value raised to the e power
s_dark = A; % Initialize;
map = A < 0.5; % Logical matrix of where A is less than 0.5.
s_dark(map) = A(map) .^ e;
% OPTION 2
% s_dark equals 0 except where A is less than 0.5 where
% it will be the values of A raised to the e power
s_dark = zeros(size(A)); % Initialize;
map = A < 0.5; % Logical matrix of where A is less than 0.5.
s_dark(map) = A(map) .^ e;

Categorías

Preguntada:

el 15 de Abr. de 2020

Comentada:

el 16 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by