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:
Array creation from data

Subject: Array creation from data

From: Mayur

Date: 8 Aug, 2013 21:07:11

Message: 1 of 4

Hi All

Lets say I have a 5 X 2 matrix in the following form:

1 0
2 3
3 10
4 2
5 1

The numbers in first columns are the size of particles and numbers in second column are their counts. Thus I have 3 particles of size 2, 10 particles of size 3 etc.

What I want to do is solve a given equation [have the program for it] for each particle and thus need to create a "for" loop.

For this I guess I need to create a array of particles sizes based on their counts.

Thus for the above example it would look something like this:

0
2
2
2
3
3
3
3
3 [ten times]
.
.
.
4
4
5

then I can run the "for loop" on the above array.

Please let me know how to create it from the data mentioned above

Thanks
Mayur

Subject: Array creation from data

From: dpb

Date: 9 Aug, 2013 00:56:00

Message: 2 of 4

On 8/8/2013 4:07 PM, Mayur wrote:
> Hi All
>
> Lets say I have a 5 X 2 matrix in the following form:
>
> 1 0
> 2 3
> 3 10
> 4 2
> 5 1
>
> The numbers in first columns are the size of particles and numbers in
> second column are their counts. Thus I have 3 particles of size 2, 10
> particles of size 3 etc.
>
> What I want to do is solve a given equation [have the program for it]
> for each particle and thus need to create a "for" loop.
>
> For this I guess I need to create a array of particles sizes based on
> their counts.
...

Why do you need the array again? Why can't you simply loop over the
length(m)

for ix=1:length(m)
   for jx=m(ix,2)
     % do whatever w/ you need to do w/ the n values of m(ix,1) here
     ...

Or, at worst,

for ix=1:length(m)
   if m(ix,2)==0, continue, end % skip the 0 count ones...
   v=repmat(m(ix,1),m(ix,2),1)); % a vector of the values...
   % do whatever w/ the vector here...

Or, at best, use

accumarray() and/or arrayfun() to eliminate the explicit loops altogether.

Need to know more about this program input to know precisely, but
doesn't sound like what you're asking is "the Matlab way"....

--

Subject: Array creation from data

From: Mayur

Date: 9 Aug, 2013 01:20:14

Message: 3 of 4

Hi

Thanks for your help.
I was looking for the looping as you did in the first example.

Thanks
Mayur

Subject: Array creation from data

From: Roger Stafford

Date: 9 Aug, 2013 06:01:32

Message: 4 of 4

"Mayur" wrote in message <ku119v$3g2$1@newscl01ah.mathworks.com>...
> The numbers in first columns are the size of particles and numbers in second column are their counts. ......
- - - - - - - - -
  As dpb has suggested, the 'accumarray' function can be used to eliminate the for-loops. Let your original n x 2 matrix be called A. You want each element in the first column of A to appear as many times as indicated by the corresponding number in A's second column. The following code does that:

 p = cumsum(accumarray(cumsum([1;A(:,2)]),1));
 B = A(p(1:end-1),1);

B is the desired column vector result.

  Note that this code doesn't give a 0 in the first element of B for your particular example. I contend that this 0 that you show in the result is inconsistent with what appears to be your intent. If the 1 is to appear zero times, there shouldn't be an entry there at all. (Also such an anomaly would be harder to achieve in just two lines of code.)

Roger Stafford

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