Trouble making a loop repeat correctly

1 view (last 30 days)
I am currently trying to solve this homework question:
This is the code I have written so far:
function y = my_insertation(x,N,m)
% only works for row matrices
% places an m amount of 0's every N places in the initial matrix
% determining size of inputted matrix
sizex = size(x);
% determining number of columns or places in the matrix
sx = sizex(2);
% initializing a matrix of zeroes with zeros
y = zeros(1,(sx+(sx*m)/N));
% determining size of created matrix
sizey = size(y);
% determining number of columns or places in matrix
sy = sizey(2);
% setting counters to 1
j = 1; % counter to place numbers of x into y
% loop that will count and add places into y matrix until full
for i = 1:(sy) % i can only go from 1 to the max amount of indexes
if i == N + 1 % determines when to place m 0's every N spaces
N_old = N
for k = 1:m % repeats placing 0's m times
y(i) = 0; % placing a zero at i
i = i + 1; % adding to the counter i
end
N = 2*N_old + m; % determining new N that will be used for the...
... next if statement
else
y(i) = x(j); % places original place of x matrix assuming the...
... function has not ended from max amount of j
j = j + 1; % increasing counter for j
i = i + 1; % increasing counter for i
end
if j == sx + 1 % failsafe to end the for loop
break
end
end
end
I think most of it is correct, and I feel pretty good about it. But I believe the one part where the code is messing up is at the point where I tell it to do the for k = 1:m. It isn't placing 0 into the y matrix twice like I'm expecting it to. I'm kind of at the end of my rope here and I don't quite know how to fix it from here.
This part specifically is where I believe it is running into problems.
if i == N + 1 % determines when to place m 0's every N spaces
N_old = N
for k = 1:m % repeats placing 0's m times
y(i) = 0; % placing a zero at i
i = i + 1; % adding to the counter i
end
N = 2*N_old + m; % determining new N that will be used for the...
... next if statement
else
y(i) = x(j); % places original place of x matrix assuming the...
... function has not ended from max amount of j
j = j + 1; % increasing counter for j
i = i + 1; % increasing counter for i
end
I would appreciate any help you could provide me, I appreciate it.
As a check, I have been using this script:
clear all
close all
clc
x = [1,3,7,2,4,9]
N = 3;
m = 2;
my_insertation(x,N,m)
% should equal the following function
xsolution = [1,3,7,0,0,2,4,9,0,0]

Accepted Answer

Walter Roberson
Walter Roberson on 16 Apr 2020
for i = 1:(sy) % i can only go from 1 to the max amount of indexes
%stuff
i = i + 1; % adding to the counter i
%more stuff
end
When you change a loop control variable inside a loop, the next iteration of the loop will change it back.
In MATLAB,
for variable = lower_bound : increment : upperbound
%stuff that might assign to variable
end
is implemented similar to
hidden_variable_lower_bound = lower_bound;
hidden_variable_increment = increment;
hidden_variable_upperbound = upperbound;
hidden_variable_value = hidden_variable_lower_bound
if isnan(hidden_variable_lowerbound) || isnan(hidden_variable_increment) || isnan(hidden_variable_upperbound)
variable = nan;
%special case, loop is executed once
%stuff that might assign to variable
%notice that variable is NOT changed by the increment at this point
elseif hidden_variable_increment > 0 & hidden_variable_upperbound >= hidden_variable_lower_bound
variable = hidden_variable_value;
while hidden_variable_value <= hidden_variable_upperbound
variable = hidden_variable_value;
%stuff that might assign to variable
hidden_variable_value = hidden_variable_value + hidden_variable_increment;
%notice that variable is NOT changed by the increment at this point
end
%notice that at the end, variable is left at the last thing assigned to it,
%including possibly in the user stuff
elseif hidden_variable_increment < 0 & hidden_variable_upperbound <= hidden_variable_lowerbound
variable = hidden_variable_value;
while hidden_variable_value <= hidden_variable_upperbound
variable = hidden_variable_value;
%stuff that might assign to variable
hidden_variable_value = hidden_variable_value + hidden_variable_increment;
%notice that variable is NOT changed by the increment at this point
end
%notice that at the end, variable is left at the last thing assigned to it,
%including possibly in the user stuff
else
%either the increment was 0 or the bounds say nothing should be done
variable = [];
end
  4 Comments
Ryan Ferguson
Ryan Ferguson on 16 Apr 2020
I apologize if this is hard to implement into my code, but I am having trouble seeing how I could do this. Would I need to introduce new counters and relate them between the different parts of the code where the counters are reset after going through a loop? I thought using the input counter as the basis to calculate the output location would've been the simplest way, but it appears not. I would appreciate any more advice on implementing this into my current code.
Thank you for your response and guidance.
Walter Roberson
Walter Roberson on 16 Apr 2020
The code is much much more simple than what you have been doing. I recommend rewriting from scratch following the algorithm that I posted. The input position can be done through as a for loop. The output position would be independent.
Needed:
  • one input array
  • one parameter telling you how many zeros to insert
  • one parameter telling you how often to insert them
  • one output array. It is possible to figure out the exact output size ahead of time, but that is deluxe.
  • one counter telling you where you are in the input array
  • one counter telling you where you are in the output array
  • possibly one counter telling you how many zeros you have inserted this particular time

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!