Why variable does not take negative value in Matlab? - matlab

I'm new to Matlab.
I have these simple lines. The problem is that the sum variable does not take negative values. All variables are double.
Inp_pixel = Inp_padded(x, y);
Filter_pixel = Filter(f_row, f_col);
sum = sum + (Inp_pixel * Filter_pixel);
for example: if Filter_pixel = -1 and Inp_pixel = 150 and sum = 0. the expected result should be -150 but I get sum = 0

The problem was in Inp_pixel. This variable assigned from an uint8 2D array.
that's why this variable didn't take the negative value of the multiplication. I used cast() function to solve this problem.
for more details about this function please check this link.
MATLAB documentation page

Related

Symbolic limit calculation, numeric value desired

I am trying to calculate a limit operation of a function inside. Here is what I did:
x = 0;
f = (cos(x)*cos(h/2)*sin(h/2))/(h/2) - (sin(x)*sin(h/2)*sin(h/2))/(h/2);
limit(f,h,0)
ans =
1
limit(f,h,1)
ans =
2*cos(1/2)*sin(1/2)
I want to see what the numeric value of 2*cos(1/2)*sin(1/2) is. How do I obtain this value?
You can use double to evaluate the final expression:
double(limit(f,h,1))
ans =
0.8415
limit is a symbolic function, so it outputs symbolic functions. You can use double (or single or whatever numeric type you want) to convert to a number.

using Matlab, how to find the maximum value over a certain range?

i want to know the coding to find the maximum value, over a certain range.
i already coded like below.
f=f'
ac_yyyy_f=ac_yyyy_f'
[row,col] = ind2sub(size(ac_yyyy_f),find(ac_yyyy_f==max(ac_yyyy_f)))
but the problem is, sometimes the maximum value of Y axis choosen by my code is not what i want.
the X axis has the range of 0 to 100000 and i want the maximum between 20000 to 100000. the problem is sometimes the max value show up at the range of 0 to 20000.
How can i figure this out?
Use the max() function:
%let R be your range of values
R = [2 1 7 4];
[value, index] = max(R);
In the above example, value will be 7 and index will be 3
For more info: http://fr.mathworks.com/help/matlab/ref/max.html
I'm using a vector of random integers to stand in for your function output.
a = floor(rand(1,100000)*100);
[val, idx] = max(a(20000:100000));
You want to use the max function here to find the maximum value rather than find.
Now, the other part of the task is getting the max value from a certain part of your matrix. You can pass just a subset of a vector or matrix to a function by indexing it with a range of values. Note that idx gives you the position of val within a(20000:100000). If you need the position within a, you need to use idx+19999.
Also, you should take a look at the matrix indexing reference—there are many different and fun ways to index a matrix—because indexing is one of the most important features of matlab.
Here's the reference for the max function:
http://www.mathworks.com/help/matlab/ref/max.html
And the reference for indexing:
http://www.mathworks.com/help/matlab/math/matrix-indexing.html
You can use the max function with a subset of your array. It will return the maximum value, as well as the index where it is located. Be sure to correct the index it returns you based on your desired range. Like this:
%//create an array of 100,000 values to play with.
f=floor(rand(100000,1).*100);
%//find the max between f(20000) and f(100000)
[myMax, I] = max( f(20000:100000) );
%//correct the index based on where we started looking
%//for the max. Subtract 1 because it's MATLAB!
myIndex = I+20000-1;
This results in:
>> myMax
myMax =
99
>> myIndex
myIndex =
20045
>> f(myIndex)
ans =
99

finding the location of max and min and replacing their values in a matrix

If I generated a random 5-by-5 matrix using the code r = rand(5).
I would like to find the location of the max value and replace this value by 10, and the min value and replace the value by -10.
How can I do it?
i tried to do the following:
r=rand(5)
find(max(max(r)))
Will this line give me the correct location of max value? And if it was correct now how can I replace the value by 10?
r = rand(5);
maxr = max(r(:));%//get maximum
r(r==maxr) = 10; %// replace maximum with 10
Use logical indexing to replace the maximum value with 10.
max returns two output arguments, first the value and then the index. Using (:) to convert your matrix to a vector and linear indexing to access, you can use this code:
[value,index]=max(r(:));
r(index)=10;

Create an increasing integer alternating sequence in MATLAB / Octave

I'm trying to find a way to create a number pattern like the one below
0,1,-2,3,-4,5....
Please note: it needs to go to 200000, but I will be splitting them up into groups of 2000.
I found a formula that looks like it would work on http://oeis.org/A181983, but when I create the formula in MATLAB / Octave, the numbers don't match up:
f_num= #(x) x / (1 + x)^2;
numval = f_num(1)
numval = 0.25000
Is there another way I should be doing this?
Method #1 - Using (-1)^x
Just use a linear increment operator to go from 0 to 200000 and multiply the sequence by (-1)^(x+1) to allow the sign of the sequence to alternate:
x = 0:200000;
y = ((-1).^(x+1)) .* x;
The addition of the +1 is important so that the even positions get a positive sign while the odd positions get a negative sign.
Method #2 - Using indexing
Alternatively, you can declare the same array from 0 to 200000, index into every even position and negate the sign:
x = 0:200000;
x(2:2:end) = -x(2:2:end);
Method #3 - Using trigonometry and integers
One more to throw into the mix. You know that for cos(x*pi), the output is -1 when x is odd and the output is 1 when x is even. We need to flip this for your case and ultimately use this alternating sequence to multiply with the same array going from 0 to 200000, and therefore:
x = 0:200000;
y = (-cos(x*pi)).*x;
Aside
Interestingly enough, (-1)^x is also equal to exp(i*pi*x) for all values of x that are integer. We can verify this by using Euler's formula where: exp(i*pi*x) = cos(pi*x) + i*sin(pi*x). Since i*sin(pi*x) = 0 for all x belonging to an integer, we really get exp(i*pi*x) = cos(pi*x). Substituting even numbers of x will give us 1 while odd numbers of x will give us -1, and hence exp(i*pi*x) = cos(pi*x) = (-1)^x for all x belonging to integers.
Also, (-1)^(x+1) = -(-1)^x = -cos(x*pi) for all x belonging to integers and so the first method is really equal to the third method anyway!
try
f_num= #(x) x * (-1)^(x+1);

Summing 3d array matlab

x=imread('test.jpg');
imshow(x);
total = 0;
for i=1:2
for j=1:2
for k=1:2
total = total + abs(x(i,j,k));
end
end
end
total
The above code prints total as 255 no matter what are the max values for i,j,k. Please explain
It prints out 255 because matlab doesn't overflow integers, and the datatype is uint8
a = repmat(uint8(100),5, 1)
a(1)+a(2)
a(1)+a(2)+a(3)
The outputs will be 200 and 255, because Matlab clamps the output at the maximum value, rather than wrapping around. If you use the sum function as given by Dennis, then you get the correct value as Matlab converts to double first
sum(a)
should give 500 as the output.
Not sure what your code fragment is, but if you want to sum of the absolute values of the array it is really easy:
sum(abs(x(:)))
If you just want the submatrix containing the first 2 values from the corner:
subM= x(1:2,1:2,1:2)
sum(abs(subM(:)))