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:
reshaping array

Subject: reshaping array

From: Leyo Joseph

Date: 3 Feb, 2012 18:11:29

Message: 1 of 5

Hello,

I have an array:

A=['A1' 'A2' 'A3' 'A4' 'A5' 'A6' 'A7' 'A8' ]
want to create another array B=['A2A1' 'A4A3' 'A6A5' 'A8A7']

is it possible to do this with the reshape command?. If not how to implement this?

Subject: reshaping array

From: dpb

Date: 3 Feb, 2012 18:37:53

Message: 2 of 5

On 2/3/2012 12:11 PM, Leyo Joseph wrote:
> Hello,
>
> I have an array:
>
> A=['A1' 'A2' 'A3' 'A4' 'A5' 'A6' 'A7' 'A8' ]
> want to create another array B=['A2A1' 'A4A3' 'A6A5' 'A8A7']
>
> is it possible to do this with the reshape command?. If not how to
> implement this?

It doesn't occur to me otomh altho I won't say "no"...

I'd brute-force it...

for idx=1:4:length(A)-4
   b(idx:idx+1)=A(idx+2:idx+3);
   b(idx+2:idx+3)=A(idx:idx+1);
end

--

Subject: reshaping array

From: Roger Stafford

Date: 3 Feb, 2012 19:50:45

Message: 3 of 5

"Leyo Joseph" wrote in message <jgh80h$m6h$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have an array:
>
> A=['A1' 'A2' 'A3' 'A4' 'A5' 'A6' 'A7' 'A8' ]
> want to create another array B=['A2A1' 'A4A3' 'A6A5' 'A8A7']
>
> is it possible to do this with the reshape command?. If not how to implement this?
- - - - - - - -
  The answer to your first question is 'no'. The '8' character must always remain at the end under reshaping (and under transposing as well.)

  For the second question an alternative without for-loops is:

 n = 0:length(A)-1;
 B = A(3+n+2*(mod(n,2)-mod(n,4)));

Roger Stafford

Subject: reshaping array

From: dpb

Date: 3 Feb, 2012 21:08:30

Message: 4 of 5

On 2/3/2012 1:50 PM, Roger Stafford wrote:
...

> For the second question an alternative without

[explicit]

> for-loops is:

<VBG>

--

Subject: reshaping array

From: james bejon

Date: 5 Feb, 2012 01:22:15

Message: 5 of 5

I suppose if you're really determined to use reshape (and to be completely inefficient and unclear to boot)

a = ['A1' 'A2' 'A3' 'A4' 'A5' 'A6' 'A7' 'A8' ];
b = regexprep( reshape( fliplr( reshape(strrep(a, 'A', ''), [], 4) .' ) .', 1, [] ), '(\d)', 'A$1' )

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