Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reading from beginning and end of an array and repeat

Subject: Reading from beginning and end of an array and repeat

From: enviro

Date: 22 Feb, 2009 14:53:14

Message: 1 of 9

Hello everyone
I want to select an array(s)with length n out of
a bigger array (z) in a way that the
s =[162 320 169 280 172]'
or(get one from first row and one from end and repeat until it reaches n)
How to write a code to get array s?
below is what I tried.

z=[162 169 172 184 199 205 220 240 280 320]'
n=5;
[p,q]=size(z);
s=zeros(n,1);
[d,e]=size(s);


for i=1:p;
    for j=p:p-(i-1);
        for d=1:n;
            for f=d+1:n;
                s(d)=z(i);
                s(f)=z(j);
            end
        end
    end
end
s

Subject: Reading from beginning and end of an array and repeat

From: Jos

Date: 22 Feb, 2009 16:30:18

Message: 2 of 9

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <33098052.1235314424604.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hello everyone
> I want to select an array(s)with length n out of
> a bigger array (z) in a way that the
> s =[162 320 169 280 172]'
> or(get one from first row and one from end and repeat until it reaches n)
> How to write a code to get array s?
> below is what I tried.
>
> z=[162 169 172 184 199 205 220 240 280 320]'
> n=5;
> [p,q]=size(z);
> s=zeros(n,1);
> [d,e]=size(s);
>
>
> for i=1:p;
> for j=p:p-(i-1);
> for d=1:n;
> for f=d+1:n;
> s(d)=z(i);
> s(f)=z(j);
> end
> end
> end
> end
> s

???

I do not understand what you want. Can you please rephrase your problem? And given an example of the input and expected output?

Jos

Subject: Reading from beginning and end of an array and repeat

From: enviro leader

Date: 22 Feb, 2009 16:44:02

Message: 3 of 9

"Jos " <#10584@fileexchange.com> wrote in message <gnruiq$cr2$1@fred.mathworks.com>...
> enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <33098052.1235314424604.JavaMail.jakarta@nitrogen.mathforum.org>...
> > Hello everyone
> > I want to select an array(s)with length n out of
> > a bigger array (z) in a way that the
> > s =[162 320 169 280 172]'
> > or(get one from first row and one from end and repeat until it reaches n)
> > How to write a code to get array s?
> > below is what I tried.
> >
> > z=[162 169 172 184 199 205 220 240 280 320]'
> > n=5;
> > [p,q]=size(z);
> > s=zeros(n,1);
> > [d,e]=size(s);
> >
> >
> > for i=1:p;
> > for j=p:p-(i-1);
> > for d=1:n;
> > for f=d+1:n;
> > s(d)=z(i);
> > s(f)=z(j);
> > end
> > end
> > end
> > end
> > s
>
> ???
>
> I do not understand what you want. Can you please rephrase your problem? And given an example of the input and expected output?
>
> Jos
-------------------------------------------------------------
input=[162 169 172 184 199 205 220 240 280 320]'
output=[162 320 169 280 172]'
I want to select 5 number from input as above. selection must happens (input(1),
input(10),input(2),input(9),input(3)...).
Please let me know if it is not clear. thanks in advance

Subject: Reading from beginning and end of an array and repeat

From: Matt Fig

Date: 22 Feb, 2009 17:00:22

Message: 4 of 9

input=[162 169 172 184 199 205 220 240 280 320]
outputlong = reshape([input;input(end:-1:1)],1,[])

% Now take however many you want.

output = outputlong(1:5)






byiGy[[ibfnyc]hyfiny[\hb?o[sy_[[p(4Soe:y!ni\a]ijg[_i_!jyg_c

Subject: Reading from beginning and end of an array and repeat

From: Roger Stafford

Date: 22 Feb, 2009 17:17:02

Message: 5 of 9

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <33098052.1235314424604.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hello everyone
> I want to select an array(s)with length n out of
> a bigger array (z) in a way that the
> s =[162 320 169 280 172]'
> or(get one from first row and one from end and repeat until it reaches n)
> How to write a code to get array s?
> below is what I tried.
>
> z=[162 169 172 184 199 205 220 240 280 320]'
> n=5;
> [p,q]=size(z);
> s=zeros(n,1);
> [d,e]=size(s);
>
>
> for i=1:p;
> for j=p:p-(i-1);
> for d=1:n;
> for f=d+1:n;
> s(d)=z(i);
> s(f)=z(j);
> end
> end
> end
> end
> s

  Do this:

 n = 5;
 m = size(z,2);
 s = z((2*m+3+(2*m-2*(1:n)+1).*(-1).^(1:n))/4);

Roger Stafford

Subject: Reading from beginning and end of an array and repeat

From: enviro

Date: 23 Feb, 2009 13:10:41

Message: 6 of 9

Thanks Roger,

Could you please let me know where I can find further readings on this kind of engines?

enviro

Subject: Reading from beginning and end of an array and repeat

From: Roger Stafford

Date: 24 Feb, 2009 07:32:04

Message: 7 of 9

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <24635655.1235394672434.JavaMail.jakarta@nitrogen.mathforum.org>...
> Thanks Roger,
>
> Could you please let me know where I can find further readings on this kind of engines?
>
> enviro

  I don't quite know how to answer your question. I just looked at the sequence

 1 m 2 m-1 3 m-2 4 m-3 ....

of indices you needed to accomplish your task, and looked for some pattern one could generate easily. I know from experience that (-1)^(1:n) has useful properties for producing alternating features in a sequence. I finally set down the equations

 a*(1:4) + b + (c*(1:4)+d)*(-1)^(1:4) = [1 m 2 m-1]

which gives four equations and four unknowns and lo and behold that solution also held true for all the other indices in (1:n). (I had an intuitive feeling it would work before I tried it.)

  I have no idea where one can read about such things. I am depending on things I have learned over a period of many years.

Roger Stafford

Subject: Reading from beginning and end of an array and repeat

From: Jos

Date: 24 Feb, 2009 11:51:02

Message: 8 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gns1ae$dk5$1@fred.mathworks.com>...

> n = 5;
> m = size(z,2);
> s = z((2*m+3+(2*m-2*(1:n)+1).*(-1).^(1:n))/4);

Another way of creating the indices IDX into z:

z = 10:20 ;
n = 5 ;

a = rem(1:n,2)
b = ceil((1:n)/2)
idx = a .* b + ~a .* (numel(z)-b+1)
z(idx)

Jos

Subject: Reading from beginning and end of an array and repeat

From: enviro

Date: 7 Mar, 2009 12:53:41

Message: 9 of 9

Is it possible to make a loop to do this? I should go that way as it is needed to check pair values for my original script. thanks

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us