How to write this code in matlab?

  • MATLAB
  • Thread starter vikky_manit
  • Start date
  • Tags
    Code Matlab
The expected output is to calculate the value of G for each pixel based on the formula provided, using the values of M0, var0, and V. However, the current code is not producing the desired results. In summary, the conversation discusses a code in MATLAB that is meant to calculate the value of G for each pixel in an image using the provided formula. However, the code is not working as expected and is only outputting a value of 100 for all pixels. The user requests assistance in correcting the code and clarifies that temp(i,j) is a matrix containing pixel values and the expected output is to calculate G for each pixel.
  • #1
vikky_manit
3
0
M0=100;
var0=100;

i=1:478
j=1:448
if(temp(i,j)>M)
G(i,j)=M0+sqrt(double((var0*power((temp(i,j)-M),2))/V));
else

G(i,j)=M0-sqrt(double((var0*power((temp(i,j)-M),2))/V));

Where temp(i,j) is an matrix. This code is not working. I am getting the values of G as 100. Please help me with the correct code. I am new to matlab.
 
Physics news on Phys.org
  • #3
What is temp()? What is your expected output?

In code tags:
Code:
M0=100;
var0=100;

[color=#008000]i[/color]=1:478;
[color=#008000]j[/color]=1:448;
[color=#008000][b]if[/b][/color](temp([color=#008000]i[/color],[color=#008000]j[/color])[color=#666666]>[/color]M)
    G([color=#008000]i[/color],[color=#008000]j[/color])=M0[color=#666666]+[/color][color=#008000]sqrt[/color](double((var0[color=#666666]*[/color]power((temp([color=#008000]i[/color],[color=#008000]j[/color])[color=#666666]-[/color]M),2))[color=#666666]/[/color]V));
[color=#008000][b]else[/b][/color]
    G([color=#008000]i[/color],[color=#008000]j[/color])=M0[color=#666666]-[/color][color=#008000]sqrt[/color](double((var0[color=#666666]*[/color]power((temp([color=#008000]i[/color],[color=#008000]j[/color])[color=#666666]-[/color]M),2))[color=#666666]/[/color]V));
[color=#008000][b]end[/b][/color]
 
  • #4
temp(i,j) is an matrix containing pixel values of an image.
 
  • #5


To write this code in matlab, you can follow these steps:

1. Define the variables M, M0, var0, V, and temp.

M = 100; % mean value
M0 = 100; % initial value of G
var0 = 100; % initial variance
V = 1; % constant value
temp = rand(478,448); % create a random matrix for temp

2. Define the loop variables i and j.

i = 1:478;
j = 1:448;

3. Use a nested for loop to iterate through each element of the temp matrix.

for i = 1:478
for j = 1:448
% check if temp(i,j) is greater than M
if temp(i,j) > M
% calculate G using the given formula
G(i,j) = M0 + sqrt((var0*(temp(i,j)-M).^2)/V);
else
% calculate G using the given formula
G(i,j) = M0 - sqrt((var0*(temp(i,j)-M).^2)/V);
end
end
end

4. Display the resulting matrix G.

disp(G);

Note: The code provided in the question is not working because the variable V is not defined and the power function is not used correctly. Also, the sqrt function should be applied to the entire expression inside the parentheses. I hope this helps!
 

1. How do I define variables in Matlab?

To define a variable in Matlab, you can use the assignment operator (=) followed by the variable name and its value. For example, to define a variable "x" with a value of 5, you can type "x = 5" in the command window.

2. How do I write a for loop in Matlab?

To write a for loop in Matlab, you can use the syntax "for variable = start_value : end_value". Within the loop, you can specify the actions to be repeated for each iteration. For example, "for i = 1 : 10" will repeat the code within the loop 10 times, with the variable "i" taking on the values 1, 2, 3, ..., 10.

3. What is the difference between single quotes and double quotes in Matlab?

In Matlab, single quotes ('') are used to enclose characters or strings, while double quotes ("") are used to enclose arrays of characters or strings. For example, 'a' is a character in single quotes, while "abc" is an array of characters in double quotes.

4. How do I plot a graph in Matlab?

To plot a graph in Matlab, you can use the plot function, specifying the x and y values as arguments. For example, "plot(x,y)" will plot a graph with x values on the x-axis and y values on the y-axis. You can also customize the graph by adding labels, titles, legends, and changing the color and style of the plot.

5. How do I read data from a file in Matlab?

To read data from a file in Matlab, you can use the importdata function. This function reads the data from the file and stores it in a structure array, which can then be accessed using the dot notation. You can also specify the delimiter used in the file, the number of header lines, and the data format.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
Back
Top