function surround = surroundWith(a,b)
%function will take a given matrix 'a' and create a ring of numbers 'b' around
%it
surround = zeros(size(a,1)+2,size(a,2)+2); %sets the size of the matrix after the numbers b has been placed around 'a'
for ii = 1:size(a,1)+2
for jj = 1:size(a,2)+2
if (ii == 1 || ii == size(a,1)+2 || jj == 1 || jj == size(a,2)+2)
surround(ii,jj) = b; %setting the ring of pre-allocated zeros around 'a' to be 'b'
end
end
end
for kk = 1: size(a,1)
for ll = 1:size(a,2)
surround(kk+1,ll+1) = a(kk,ll);%setting the zeros within b to the numbers stored in 'a' at the corresponding locations
end
end
end
% if a = [1,2,3:4,5,6], and b = 5 the matrix should come out as:
5 5 5 5 5
5 1 2 3 5
5 4 5 6 5
5 5 5 5 5
im attempting to recreate this using only one parameter and to create a matrix with a 1 at the centre with a ring of numbers layered on top of eachother, increasing in value up to 'b' as the distance from the centre increases so:
%so layeredMatrix(5) should output:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
ive been having a lot of trouble with this and would appriciate any help

 Réponse acceptée

Geoff Hayes
Geoff Hayes le 3 Mai 2019
Tristan - why not use a for loop
surround = 1;
for k = 2:5
surround = surroundWith(surround, k);
end

5 commentaires

Tristan's answer moved here
im trying to write a new function such that the output is makeLayered(a)
without relying on the function surroundWith that iv'e already written. I was told the new code I have to write uses similar information to surroundWith, however I have been unable to do so for after trying for some time.
what im trying to do is
function layered = makelayered(a)
%put in code here for the function
end
% for example makeLayered(5) should output
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
%without having to rely on using surroundWith(a,b) to make it so
Geoff Hayes
Geoff Hayes le 3 Mai 2019
I was told the new code I have to write uses similar information to surroundWith,
Were you told anything else? Use recursion perhaps? Why can the above function not call the other? I'm guessing this is homework so there must be some algorithm that the instructor is hoping you will use.. If you have any more information, please provide it.
Tristan Murphy
Tristan Murphy le 3 Mai 2019
no, as far as i know we are supposed to use our previous solution as a hint (which is the surroundWith function). in the description it does say this solution would be very helpful, and does not specify that we are not allowed to call it. however I do wish to find a method in which i can make the layered matrix without the requirement of calling the surroundWith function. either way i appriciate the help.
Geoff Hayes
Geoff Hayes le 3 Mai 2019
Modifié(e) : Geoff Hayes le 3 Mai 2019
since you are told that this solution would be very helpful then I don't understand why you can't use it. :)
function layered = makelayered(a)
layered = 1;
for k = 2:a
layered = surroundWith(layered, k);
end
end
Tristan Murphy
Tristan Murphy le 3 Mai 2019
alrighty thanks for all the help :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by