how to find all integers between two integers

13 ビュー (過去 30 日間)
Alisha Ali
Alisha Ali 2015 年 5 月 2 日
コメント済み: Adwaith G 2022 年 6 月 16 日
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 件のコメント
John D'Errico
John D'Errico 2015 年 5 月 2 日
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
Alisha Ali 2015 年 5 月 2 日
i tried this
function v = int_col(n) v = [n:-1:1]' end
the solver gives error when input is 3

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2015 年 5 月 2 日
Ok, so your solution works when n is an even integer. For an odd integer, the middle element of that string will cause it to fail, because it stays unchanged in the permutation you have chosen.
You might also choose a random permutation. Something like this:
vec = 1:n;
ind = 1:n;
while any(vec == ind)
vec = vec(randperm(n));
end
The problem is this might take a while. Have you ever seen the birthday paradox? This is actually a variation of that paradox, in that you will get hits surprisingly often for even moderately sized values of n. (A nice question is how large does n need be so that you will see a problem in more than 50% of the random samplings that you take? What value of n assures a hit over 99% of the time?)
But here is a simple solution that will never fail. Suppose you took the last number, and put it first? So, for example,
[6 1 2 3 4 5]
or
[3 1 2]
You get the idea. Just do a circular shift on the vector. Is there a function in MATLAB that does such a thing? Try this:
lookfor circular
Does it show any functions in MATLAB that will do a circular shift?
Of course, this is simple enough to do without a built-in function. So you might try this:
[n,1:(n-1)]
  3 件のコメント
John D'Errico
John D'Errico 2015 年 5 月 2 日
As you can see, when you make a credible effort, you get help.
Adwaith G
Adwaith G 2022 年 6 月 16 日
function v = int_col(n)
v = circshift((1:n)',1); % Transposed it , to make it a column vector and then shifted by 1
Is this correct or not? (I am very knew to MATLAB , so i am sorry for any dumb comments or doubts

サインインしてコメントする。

その他の回答 (5 件)

Roger Stafford
Roger Stafford 2015 年 5 月 2 日
Another hint: Check out the 'circshift' function.

Image Analyst
Image Analyst 2015 年 5 月 2 日
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
Muhammad Usman Saleem 2015 年 5 月 9 日
編集済み: Muhammad Usman Saleem 2015 年 5 月 9 日
@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 件のコメント
John D'Errico
John D'Errico 2015 年 5 月 9 日
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
srinivas 2015 年 5 月 11 日
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 件のコメント
srinivas
srinivas 2015 年 5 月 11 日
Try this
Stephen23
Stephen23 2015 年 5 月 11 日
編集済み: Stephen23 2015 年 5 月 11 日
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
Thomas Nguyen 2018 年 4 月 5 日
function[v] = int_col(n)
%Const:
%n=some_int_here;
v=size(n);
for i=1:n-1
v(i+1)=i;
end
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by