# How to find vector with if loops and for loops

44 views (last 30 days)
Ömer Durkut on 15 Jun 2022
Edited: Torsten on 24 Jun 2022
Given a vector u with the dimension 20x1, which consists of random variables from a uniform distribution (distributed on [0,1]) exists. Instructions: • Create a code in MATLAB, – which creates the vector u, – which contains both ’for loops’ and ’if loops’, – and for all values in the vector u indicates in which quarter the number lies.This is one of my mathlab final exam question, could you please help me,i just know that i should use "rand" function
Ömer Durkut on 15 Jun 2022
Thanks a lot

Image Analyst on 16 Jun 2022
Are you allowed to turn in other people's solutions as your own for your final exam question?
The first 5 values of the 20-element u lie in the first quarter, the next 5 in the second quarter, the third 5 in the third quarter, and the final 5 elements in indexes 16-20 obviously lie in the last quarter. But I think they want you to use a for loop
u = rand( % You said you got this
for k = 1 : length(u)
if u(k)...........
More code
end
end
You can either store the quarter that the number lives in, in a vector called quarter, or maybe you just want to print out the quarter it's in using fprintf(). Or maybe you want to do both.
Torsten on 24 Jun 2022
I like if the order of the conditions in if-statements can be changed.
This is not the case in this simplified version because
if r(k) < 0.75
quarter(k) = 3;
elseif r(k) < 0.25
quarter(k) = 1;
elseif r(k) < 0.5
quarter(k) = 2;
else
quarter(k) = 4;
end
would create chaos.

Masnat on 23 Jun 2022
Edited: Torsten on 23 Jun 2022
This answer was flagged by Image Analyst
clear all;
close all;
%Define number of samples to take
fs = 8000;
% Sampling frequency
f = 400; %Hz
%Define signal
t = 0:1/fs:1-1/fs;
A = 3.0;
powfund = A^2/2 ;
s = 0.1;
varnoise = s^2;
signal = 20*sin(2*pi*300*t)+20*cos(2*pi*900*t)+11*randn(size(t));
%noise
noise = s*randn(size(signal));
%noisy signal
noisySignal = signal + noise;
SNR = snr(noisySignal) %Calculation of SNR using snr function
SNR = 2.3205
defSNR = 10*log10(powfund/varnoise) %Calculation of SNR following the definition
defSNR = 26.5321
Image Analyst on 23 Jun 2022
I think you posted this in the wrong discussion thread. This discussion has nothing to do with SNR.