how to find all integers between two integers
Show older comments
Write a function called int_col that has one input argument, a positive integer n that is greater than 1, and one output argument v that is a column vector of length n containing all the positive integers smaller than or equal to n, arranged in such a way that no element of the vector equals its own index. In other words, v(k) is not equal to k for any valid index k.
2 Comments
John D'Errico
on 2 May 2015
Ok, so you managed to type in your homework assignment. Good for you. Of maybe you just did a copy and paste.
Why not make an effort? After all if you cannot do so, you will never learn MATLAB. Learning involves using the language. Making an effort, even if you don't succeed, you learn by trying. This is what homework is for. If we do it for you, then all you learn is how to con someone else into doing your work for you. And while that may be laudable for some people, it is not so here.
So try it out. Start writing. And when you DO have a question when you get stuck, THEN ask it, and show what you tried, and explain what failed. You will probably get some help then.
Alisha Ali
on 2 May 2015
Accepted Answer
More Answers (5)
Roger Stafford
on 2 May 2015
1 vote
Another hint: Check out the 'circshift' function.
Image Analyst
on 2 May 2015
0 votes
Hint: read up on the colon operator, fliplr(), the == operator, and the ' (transpose) operator (to turn row vectors into column vectors). Also, read this: http://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers The rem() function may come in handy to determine if n is even or odd.
Muhammad Usman Saleem
on 9 May 2015
Edited: Muhammad Usman Saleem
on 9 May 2015
@alisha ali , take my codes and guide where is the need for correction.. thanks in advance for assistance
function v=int_col(n)
v=1:n;
for i=1:n
if v(i)==i
v(i)=[];
end
end
end
end
1 Comment
John D'Errico
on 9 May 2015
This code will not actually satisfy the requirement that all n integers will be in the list, just in a permuted order.
In fact, what this code will do is delete the FIRST element, then leave the rest of them unchanged in order, but shifted. (Look carefully at what it does. Think about it.) Therefore, your vector of integers will never have the number 1 in it.
Remember that the requirement is for a vector that contains "all the positive integers smaller than or equal to n". So deleting integers that fail is not the correct idea. Look instead at my answer.
srinivas
on 11 May 2015
function [v]=int_col(n)
if n<3
v=[2;1];
else
v=1:n;
x=v(3:n);
x1=transpose(x);
k1=[1;2];
y=cat(1,x1,k1);
v=y;
end
end
2 Comments
srinivas
on 11 May 2015
Try this
This main part of this submission:
>> int_col(5)
ans =
3
4
5
1
2
can be replaced with one simple line:
>> n = 5;
>> [3:n,1:2].'
ans =
3
4
5
1
2
Thomas Nguyen
on 5 Apr 2018
function[v] = int_col(n)
%Const:
%n=some_int_here;
v=size(n);
for i=1:n-1
v(i+1)=i;
end
end
Categories
Find more on Logical 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!