how to store the conditional loop data
2 views (last 30 days)
sir/ madam please correct me.
utt(:,1)=ut(n,1); %% i want to store the ut as a utt in a column
Star Strider on 29 Apr 2022
If I understand the code correctly, it can be simply stated as:
U_y = [u_y;u_y;u_y;u_y;u_y]; % Create Column Vector
utt = ut; % Copy 'ut' to 'utt'
utt(ut>U_y) = 0 % Set Appropriate Elements To Zero
More Answers (1)
Jan on 29 Apr 2022
Your cleaned code:
ut = rand(15, 1);
uy = [0.84; 0.384; 0.784];
U_y = repmat(uy, 5, 1);
for n = 1:15
if ut(n) > U_y(n)
ut(n) = 0;
utt(n) = ut(n);
% ^ insert an index here
Remember, that X(k,1) can be written as X(k), which looks a little bit cleaner.
Instead of the loop, you can write:
utt = ut .* (ut <= U_y);
The expression in the parentheses is a vector of 1s and 0s and the elementwise multiplication set the wanted elements to 0.