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:
Combining elements of an array..

Subject: Combining elements of an array..

From: DRG

Date: 14 Jun, 2012 14:40:15

Message: 1 of 2

Greetings all. I have a code which the user specifies a number N at the beginning to specify a fraction. Goes like this

N = input('Specify number of fractions: ');

f = 80 / N; %divides the total number into fractions

d = 1:1:f ; %makes chunks of that fraction

for m = 1:N;
    
 s(d,m) = (exp((-5.*d) -(7.*(d.^2)))).^m ;
     
    m = m +1;
    
end

This gives me an array and this is fine. When I choose my fraction to be 10, for example, I get chunks of 8. But what I'm interesting in doing in making a new array from this, sequentially combining the results of the two dimensional array into a linear one; for the example of 8, I can do this long form by;


Total = [s(:,1)' s(:,2)' s(:,3)' s(:,4)' s(:,5)' s(:,6)' s(:,7)' s(:,8)' s(:,9)' s(:,10)'];

It works, but is there a way to generalise this method and include it in the loop? Otherwise it's clunky.

Thanks!

DRG

Subject: Combining elements of an array..

From: dpb

Date: 14 Jun, 2012 19:31:48

Message: 2 of 2

On 6/14/2012 9:40 AM, DRG wrote:
> Greetings all. I have a code which ... Goes like this
>
> N = input('Specify number of fractions: '); f = 80 / N; %divides the
> total number into fractions d = 1:1:f ; %makes chunks of that
> fraction for m = 1:N; s(d,m) = (exp((-5.*d) -(7.*(d.^2)))).^m ;

% Don't do this--m is the loop variable and will increment automagically
% Matlab is smart enough it won't allow it anyway, but don't fool w/
% the natural scheme of things...
> % m = m +1; end
>
> This gives me an array and this is fine.

Oh, really???? I get
>> N=8; f = 80 / N; %divides the total number into fractions
d = 1:1:f ; %makes chunks of that fraction
for m = 1:N;
  s(d,m) = (exp((-5.*d) -(7.*(d.^2)))).^m ;
end
??? In an assignment A(matrix,matrix) = B, the number of rows in B
and the number of elements in the A row index matrix must be the same.

>>

If I assume you meant to write 'd' for 'm', then it'll run but there's
only a single value that isn't <<<1 and 90% or so --

>> s(10)
ans =
   9.8542e-034
>> s(2)
ans =
   1.6285e-266
>> sum(sum(s~=0))
ans =
     11
>>

Seems unlikely to be of much use???


> ... When I choose my fraction to be 10, for example, I get chunks of
> 8. But what I'm interesting in doing in making a new array from
> this, sequentially combining the results of the two dimensional array
> into a linear one; for the example of 8, I can do this long form by;
>
> Total = [s(:,1)' s(:,2)' s(:,3)' ... s(:,8)' s(:,9)' s(:,10)'];
>
> It works, but is there a way to generalise this method...

Questions of the purpose/usefulness aside, that's just

T=reshape(s',mumel(s),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