How to create a vector in which numbers increase at first, then remain constant, then reduce back to 1 ?

17 vues (au cours des 30 derniers jours)
I need to create vectors of length 255 which have the following format:
{1,2,3,4,5,5,5,5,......,5,5,4,3,2,1}
{1,2,3,4,5,6,6,6,....,6,6,5,4,3,2,1}
what i did was, create a for loop and then use a series of while loops as shown below :
for l=1:255
count=1;
%insert the increasing numbers, numbers go upto l
while count<=l
ul(count)=count;
count=count+1;
end
%After reaching l, they should be constant
while count<(255-l)
ul(count)=l;
count=count+1;
end
%Last part where numbers should decrease to 1
x=0;
while count>(254-l)&&count<256
ul(count)=l-x;
x=x+1;
count=count+1;
end
U(l,:)=ul;
end
The problem : 1- in the output, instead of getting 1 as the 255th element, i am getting a 0
2- after l=128(half of 255), the series should go upto 128 and reduce back to 1. Instead it goes above 128 and then starts reducing.
  1 commentaire
Navid
Navid le 25 Avr 2014
lets say that you want to create a vector length "m" and want to go up to "n" and then be constant and then decrease:
function y=increasedecreasefun(m,n)
A=n*ones(1,m);
for i=1:n-1
A(i)=i;
A(m-i+1)=i;
end
y=A
end

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 25 Avr 2014
Modifié(e) : Image Analyst le 25 Avr 2014
How about:
% Do for n=5,6,7,8,9.
% Can extend to other numbers
% Method #1: put into a cell array.
for n = 5:9
U{n-4} = [1:n, 5*ones(1,255-2*n), n:-1:1]
end
% Method #2: put into a 2D array.
U2D = zeros(5, 255);
for n = 5:9
U2D(n-4,:) = [1:n, 5*ones(1,255-2*n), n:-1:1]
end
[EDITED TO SHOW ALTERNAT, 2-D ARRAY METHOD]
  1 commentaire
Shounak Shastri
Shounak Shastri le 26 Avr 2014
Thanks.... Got a clue from your first answer itself. But your Method 2 also works. This is what i used in my program
for l = 1:255
if l<(255/2)
u(l,:) = [1:l, l*ones(1,255-2*l), l:-1:1];
else
u(l,:) = [1:128, 127:-1:1];
end
end
The if-else block is to accommodate the problem #2. The matrix u stores the series till l=128. After l=128, it goes up till 128 and reduces back to 1.

Connectez-vous pour commenter.

Plus de réponses (2)

Youssef  Khmou
Youssef Khmou le 25 Avr 2014
To accomplish this task without loop, you need two variables, the maximum value m and the number of redundancy n :
First example :
m=5;
n=245;
A=[1:m m*ones(1,n) m:-1:1];
Second example :
m=6;
n=255-12;
B=[1:m m*ones(1,n) m:-1:1];
  3 commentaires
Youssef  Khmou
Youssef Khmou le 25 Avr 2014
Modifié(e) : Youssef Khmou le 25 Avr 2014
no, this technique reduces N to N-1 loops.
Shounak Shastri
Shounak Shastri le 26 Avr 2014
Won't I have to implement a loop to calculate n for 255 iterations ? Otherwise I can create an array for all values of n
n=255:-2:1
and then do the rest using each element of n.

Connectez-vous pour commenter.


Jos (10584)
Jos (10584) le 25 Avr 2014
If N is your fixed length and M is the maximum number (-> (1 2 … M-1 M M M M-1 … 2 1"), here is a simply code:
N = 20 , M = 6
A = min(N/2-abs((0:N)-N/2)+1, M)
To get all the arrays for M is 1 up till (N+1)/2, try this:
N = 11
AA = bsxfun(@min, N/2-abs((0:N)-N/2)+1, (1:(N/2)+1).') % AA is a ceil(N+1)/2-by-N matrix

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by