Append in Julia - append

I am new to Julia. A was curious how do I append values. I want b to grow every time the boolian value is True. And then to output its size.
function f(img_deformed, img)
s = size(img)[1]
for i in range(1,s,step=1)
for j in range(1,s,step=1)
b = img_deformed[i,j] == img[i,j]
end
end
return b
end

If you want b to be a vector that tracks the number of times that the equality in your for loop is satisfied, you can use push!:
function f(img_deformed, img)
s = size(img)[1]
b = Vector{Bool}() # Can also use Bool[]
for i in range(1,s,step=1)
for j in range(1,s,step=1)
if img_deformed[i,j] == img[i,j]
push!(b, true)
end
end
end
return length(b)
end
However, if all you really care about is the number of trues, it's easier (and almost certainly better) to just use b as a counter:
function f(img_deformed, img)
s = size(img)[1]
b = 0
for i in range(1,s,step=1)
for j in range(1,s,step=1)
if img_deformed[i,j] == img[i,j]
b += 1
end
end
end
return b
end
Some minor style points: s = size(img)[1] is equivalent to s = size(img, 1), and the range(1, s, step=1) is equivalent to 1:s, so your code could be written slightly more simply as
function f(img_deformed, img)
s = size(img, 1)
b = 0
r = 1:s
for i in r
for j in r
if img_deformed[i,j] == img[i,j]
b += 1
end
end
end
return b
end
However, that doesn't address a potential mistake in the original code: unless you know that img will always be a square matrix, using the same range (1:s) for both for loops is not guaranteed to be correct. To avoid this problem, you can use axes:
function f(img_deformed, img)
b = 0
for j in axes(img, 2)
for i in axes(img, 1)
if img_deformed[i,j] == img[i,j]
b += 1
end
end
end
return b
end
Notice here that I've chosen to loop over the columns first; this is a good practice in Julia, since arrays are stored in column-major order (see the explanation here from the manual).
Note also that using img to control the values that we loop over implicitly assumes that size(img) == size(img_deformed). Without knowing more about what this function is intended for, it's hard to suggest how to deal with that, but if you can assume that the two matrices should be the same size, you can add a check at the top of f(), e.g.
function f(img_deformed, img)
#assert size(img) == size(img_deformed)
# rest of code is the same
end

Related

Plotting the Impulsefunction

so I'm new to Matlab and had to draw the Impulsefunction with y(n) is only 1 if n == 3, else 0. The following code works:
n = -5:5;
f = n; % allocate f
for i = 1 : length(n)
f(i) = dd1(n(i)-3);
end
stem(n, f);
function y = dd1(n)
y = 0;
if n == 0
y = 1;
end
end
But I feel like it's to complicated, so I tried the following:
n = -5:5
stem(n, fo)
function y = fo(n)
y = 0
if n == 3
y=1
end
end
This returns
Not enough input arguments.
Error in alternative>fo (line 5)
if n == 3
Error in alternative (line 2)
stem(n, fo)
I feel like I'm missing something trivial here.
if is no vector-wise operation but expects a single boolean (or at least a scalar that it can cast to a boolean).
But you can do this vector-wise:
lg = n == 3;
This produces a logical (MATLAB's name for boolean) array (because n is an array and not a vector), which is true where n is equal (==) to three. So you don't need a function, because you can make use of MATLAB's ability to work with vectors and arrays implicitly.
(for your code it would be f = (n-3) == 3)
A last hint: if you have a state-space system (ss-object), you can use the function step to get the step-response as a plot.

Output 1, 0.5, or 0 depending if a matrix elements are prime, 1, or neither

I am sending a matrix to my function modifikuj, where I want to replace the elements of the matrix with:
1 if element is a prime number
0 if element is a composite number
0.5 if element is 1
I dont understand why it is not working. I just started with MATLAB, and I created this function:
function B = modifikuj(A)
[n,m] = size(A);
for i = 1:n
for j = 1:m
prost=1;
if (A(i,j) == 1)
A(i,j) = 0.5;
else
for k = 2:(A(i,j))
if(mod(A(i,j),k) == 0)
prost=0;
end
end
if(prost==1)
A(i,j)=1;
else
A(i,j)=0;
end
end
end
end
With
A = [1,2;3,4];
D = modifikuj(A);
D should be:
D=[0.5, 1; 1 0];
In MATLAB you'll find you can often avoid loops, and there's plenty of built in functions to ease your path. Unless this is a coding exercise where you have to use a prescribed method, I'd do the following one-liner to get your desired result:
D = isprime( A ) + 0.5*( A == 1 );
This relies on two simple tests:
isprime( A ) % 1 if prime, 0 if not prime
A == 1 % 1 if == 1, 0 otherwise
Multiplying the 2nd test by 0.5 gives your desired condition for when the value is 1, since it will also return 0 for the isprime test.
You are not returning anything from the function. The return value is supposed to be 'B' according to your code but this is not set. Change it to A.
You are looping k until A(i,j) which is always divisible by itself, loop to A(i,j)-1
With the code below I get [0.5,1;1,0].
function A = modifikuj(A)
[n,m] = size(A);
for i = 1:n
for j = 1:m
prost=1;
if (A(i,j) == 1)
A(i,j) = 0.5;
else
for k = 2:(A(i,j)-1)
if(mod(A(i,j),k) == 0)
prost=0;
end
end
if(prost==1)
A(i,j)=1;
else
A(i,j)=0;
end
end
end
end
In addition to #EuanSmith's answer. You can also use the in built matlab function in order to determine if a number is prime or not.
The following code will give you the desired output:
A = [1,2;3,4];
A(A==1) = 0.5; %replace 1 number with 0.5
A(isprime(A)) = 1; %replace prime number with 1
A(~ismember(A,[0.5,1])) = 0; %replace composite number with 0
I've made the assumption that the matrice contains only integer.
If you only want to learn, you can also preserve the for loop with some improvement since the function mod can take more than 1 divisor as input:
function A = modifikuj(A)
[n,m] = size(A);
for i = 1:n
for j = 1:m
k = A(i,j);
if (k == 1)
A(i,j) = 0.5;
else
if all(mod(k,2:k-1)) %check each modulo at the same time.
A(i,j)=1;
else
A(i,j)=0;
end
end
end
end
And you can still improve the prime detection:
2 is the only even number to test.
number bigger than A(i,j)/2 are useless
so instead of all(mod(k,2:k-1)) you can use all(mod(k,[2,3:2:k/2]))
Note also that the function isprime is a way more efficient primality test since it use the probabilistic Miller-Rabin algorithme.

Matlab Image Blob Detection Using Recursive Flood Fill

I am trying to do blob detection in Matlab. How can I get rid of my infinite recursion in flood_fill().
%Input Image
blobs = im2bw(imread('Week6Image.jpg'),0.1);
disp(blobs);
[W, H] = size(blobs);
region_labeling(blobs,W,H)
function region_labeling(I,W,H)
label = 2;
for u = 1 : W
%For loop vertical Axis
for v = 1 : H
if I(u,v) == 1
flood_fill(I,u,v,label,W,H);
label = label + 1;
end
end
end
end
It gets stuck in this function due to infinite recursion. What should I do so that it breaks out at the right time.
function flood_fill(I,u,v,label,W,H)
if u >= 1 && u < W && v >= 1 && v < H && I(u,v) == 1
I(u,v) = label;
flood_fill(I,u+1,v,label,W,H);
flood_fill(I,u,v+1,label,W,H);
flood_fill(I,u,v-1,label,W,H);
flood_fill(I,u-1,v,label,W,H);
end
end
I see the problem now: you are passing I into the function, but not getting the modifications back. MATLAB passes all arguments by value (with the exception of handle classes). This means that a function can change its input arguments without changing the value of those matrices in the caller's workspace. You need to modify the flood_fill function to return I.
The other issue is that blobs is a logical array, and assigning any non-zero value to it yields a value of 1. You need to cast it to some other type, either doubles with +blobs or a specific integer type as below.
This is the corrected code:
blobs = im2bw(imread('Week6Image.jpg'),0.1);
blobs = uint16(blobs); % or uint32 if you have many regions
[W, H] = size(blobs);
out = region_labeling(blobs,W,H);
function I = region_labeling(I,W,H)
label = 2;
for u = 1 : W
%For loop vertical Axis
for v = 1 : H
if I(u,v) == 1
I = flood_fill(I,u,v,label,W,H);
label = label + 1;
end
end
end
end
function I = flood_fill(I,u,v,label,W,H)
if u >= 1 && u <= W && v >= 1 && v <= H && I(u,v) == 1
I(u,v) = label;
I = flood_fill(I,u+1,v,label,W,H);
I = flood_fill(I,u,v+1,label,W,H);
I = flood_fill(I,u,v-1,label,W,H);
I = flood_fill(I,u-1,v,label,W,H);
end
end
Note also that it is beneficial, in a double loop, to have the fastest-changing index be the inner loop. MATLAB is column-major, so in I(u,v), u is the fastest-changing index. Thus, ideally you would swap the two for loops around. But, given that this is such a slow algorithm and that MATLAB is not so efficient with recursion, you will likely not see the difference in this particular piece of code.

In matlab how to use if loop having condition (a is a scalar number and is equal to any element in a vector v of n length)

I have a function replace_me which is defined like as: function w = replace_me(v,a,b,c). The first input argument v is a vector, while a, b, and c are all scalars. The function replaces every element of v that is equal to a with b and c. For example, the command
x = replace_me([1 2 3],2,4,5); returns x as [1 4 5 3].
The code that I have created is
function w = replace_me(v,a,b,c)
[row,column]=size(v);
new_col=column+1;
w=(row:new_col);
for n=(1:column)
if a==v(n)
v(n)=b;
o=n;
d=n-1;
u=n+1;
for z=1:d
w(z)=v(z);
end
for z=u:column
w(z+1)=v(z);
end
w(o)=b;
w(o+1)=c;
end
end
end
It works perfectly fine for x = replace_me([1 2 3],2,4,5); I get required output but when I try x = replace_me([1 2 3], 4, 4, 5) my function fails.
To resolve this problem I want to use an if else statements having conditions that if a is equal to any element of vector v we would follow the above equation else it returns back the vector.
I tried to use this as if condition but it didn't worked
if v(1:column)==a
Any ideas
I'm not entirely sure if I understand what you are trying to achieve, but form what I understand you're looking for something like this:
function [v] = replace_me(v,a,b,c)
v = reshape(v,numel(v),1); % Ensure that v is always a column vector
tol = 0.001;
aPos = find( abs(v-a) < tol ); % Used tol to avoid numerical issues as mentioned by excaza
for i=numel(aPos):-1:1 % Loop backwards since the indices change when inserting elements
index = aPos(i);
v = [v(1:index-1); b; c; v(index+1:end)];
end
end
function w = move_me(v,a)
if nargin == 2
w=v(v~=a);
w(end+1:end+(length(v)-length(w)))=a;
elseif isscalar(v)
w=v;
else
w=v(v~=0);
w(end+1)=0;
end
end

Plotting own function in scilab

Hey i have an issuse with plotting my own function in scilab.
I want to plot the following function
function f = test(n)
if n < 0 then
f(n) = 0;
elseif n <= 1 & n >= 0 then
f(n) = sin((%pi * n)/2);
else
f(n) = 1;
end
endfunction
followed by the the console command
x = [-2:0.1:2];
plot(x, test(x));
i loaded the function and get the following error
!--error 21
Invalid Index.
at line 7 of function lala called by :
plot(x, test(x))
Can you please tell me how i can fix this
So i now did it with a for loop. I don't think it is the best solution but i can't get the other ones running atm...
function f = test(n)
f = zeros(size(n));
t = length(n);
for i = 1:t
if n(i) < 0 then
f(i) = 0;
elseif n(i) <= 1 & n(i) >= 0
f(i) = sin((%pi * n(i)/2));
elseif n(i) > 1 then
f(i) = 1;
end
end
endfunction
I guess i need to find a source about this issue and get used with the features and perks matlab/scilab have to over :)
Thanks for the help tho
The original sin is
function f = test(n)
(...)
f(n) = (...)
(...)
endfunction
f is supposed to be the result of the function. Therefore, f(n) is not "the value that the function test takes on argument n", but "the n-th element of f". Scilab then handles this however it can; on your test case, it tries to access a non-integer index, which results in an error. Your loop solution solves the problem.
Replacing all three f(n) by f in your first formulation makes it into something that works... as long as the argument is a scalar (not an array).
If you want test to be able to accept vector arguments without making a loop, the problem is that n < 0 is a vector of the same size as n. My solution would use logical arrays for indexing each of the three conditions:
function f = test(n)
f = zeros(size(n));
negative = (n<0);//parentheses are optional, but I like them for readability
greater_than_1 = (n>1);
others = ~negative & ~greater_than_1;
f(isnegative)=0;
f(greater_than_1)=1;
f(others) = sin(%pi/2*n(others));
endfunction