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:
Matrix variable with parametric elements

Subject: Matrix variable with parametric elements

From: Michael Doukas

Date: 26 Dec, 2012 11:51:07

Message: 1 of 11

Hello everybody,

I am fairly new to Matlab so please, if you can, reply as plain as possible.

I want to create a 40X9 matrix and store it as a variable, but the matrix elements must be "parametric", meaning that i want the elements of the:
first column to take random integer values in the range 0:2 (i.e. integers 0 or 1 or 2),
second column to take random integer values in the range 3:6 (i.e. 3 or 4 or 5 or 6),
...
ninth column to take random integer values within the range 56:67 (i.e. 56 or 57...67)

Please let me know in case you want me to further explain my problem.

Thanks,
Mike

Subject: Matrix variable with parametric elements

From: Nasser M. Abbasi

Date: 26 Dec, 2012 12:26:03

Message: 2 of 11

On 12/26/2012 5:51 AM, Michael Doukas wrote:
> Hello everybody,
>
> I am fairly new to Matlab so please, if you can, reply as plain as possible.
>
> I want to create a 40X9 matrix and store it as a variable, but the matrix elements must be "parametric", meaning that i want the elements of the:
> first column to take random integer values in the range 0:2 (i.e. integers 0 or 1 or 2),
> second column to take random integer values in the range 3:6 (i.e. 3 or 4 or 5 or 6),
> ...
> ninth column to take random integer values within the range 56:67 (i.e. 56 or 57...67)
>
> Please let me know in case you want me to further explain my problem.
>
> Thanks,
> Mike
>

How did you get that columns 9 will have 56:67? I think it should be 52:62

Subject: Matrix variable with parametric elements

From: Nasser M. Abbasi

Date: 26 Dec, 2012 12:32:47

Message: 3 of 11

On 12/26/2012 6:26 AM, Nasser M. Abbasi wrote:
> On 12/26/2012 5:51 AM, Michael Doukas wrote:
>> Hello everybody,
>>
>> I am fairly new to Matlab so please, if you can, reply as plain as possible.
>>
>> I want to create a 40X9 matrix and store it as a variable, but the matrix elements must be "parametric", meaning that i want the elements of the:
>> first column to take random integer values in the range 0:2 (i.e. integers 0 or 1 or 2),
>> second column to take random integer values in the range 3:6 (i.e. 3 or 4 or 5 or 6),
>> ...
>> ninth column to take random integer values within the range 56:67 (i.e. 56 or 57...67)
>>
>> Please let me know in case you want me to further explain my problem.
>>
>> Thanks,
>> Mike
>>
>
> How did you get that columns 9 will have 56:67? I think it should be 52:62
>

Ok, please try this. Nothing fancy, just a loop (oh no!)

-------------------
nRow = 40;
nCol = 9;
from = 0; to = 2;
A = zeros(nRow,nCol);

for j = 1:nCol
     A(:,j) = randi([from,to],nRow,1);
     from = to+1;
     to = from+j+2;
end
----------------

A =
      0 5 11 17 20 32 33 48 52
      2 6 8 17 20 32 35 47 54
      0 5 11 15 21 26 40 43 52
      1 5 11 15 21 27 33 51 54
      1 6 9 12 20 32 40 47 53
      2 3 11 17 20 29 33 46 54
      1 4 11 14 21 29 39 51 53
      1 3 9 13 22 29 37 48 53
      2 3 10 17 24 31 34 46 58
      1 5 9 16 23 29 38 50 61
      1 5 7 17 20 26 34 47 62
      2 6 9 13 23 28 39 47 54
      0 5 10 16 18 28 38 48 57
      2 3 9 15 18 32 33 45 56
      0 6 8 12 18 29 33 44 57
      2 6 11 14 19 30 34 47 54
      1 4 11 13 20 30 33 50 52
      2 5 11 16 20 27 36 46 56
      1 6 8 13 18 29 40 43 53
      1 5 9 17 21 29 38 46 52
      2 6 11 16 18 26 37 45 62
      0 4 11 14 19 29 40 46 56
      0 4 10 14 22 30 33 50 62
      2 5 8 16 24 28 41 46 60
      1 4 10 17 24 31 33 45 52
      0 5 7 15 21 30 37 45 59
      1 5 9 15 24 27 34 43 59
      0 5 10 13 21 28 38 44 59
      0 4 11 14 21 28 33 42 58
      0 3 11 16 20 31 39 46 54
      0 4 9 17 21 30 40 44 60
      0 4 10 16 21 28 41 44 54
      1 6 9 12 18 30 41 46 56
      0 6 11 16 24 32 37 43 61
      2 5 11 14 18 31 35 46 61
      0 3 11 14 21 30 33 49 56
      2 3 8 12 23 25 37 44 55
      1 5 9 16 20 28 38 49 58
      0 4 8 13 22 29 39 42 62
      1 6 10 13 23 28 33 45 62

--Nasser




  

Subject: Matrix variable with parametric elements

From: Michael Doukas

Date: 26 Dec, 2012 12:37:07

Message: 4 of 11

"Nasser M. Abbasi" wrote in message <kbeqd7$r15$1@speranza.aioe.org>...
> How did you get that columns 9 will have 56:67? I think it should be 52:62
>
>
I am defining the ranges that each column may take.
There is no need to be of the same length for my purposes, they just need to increment with a step of 1.
I do not see any problem with that.
 

Subject: Matrix variable with parametric elements

From: Nasser M. Abbasi

Date: 26 Dec, 2012 12:44:34

Message: 5 of 11

On 12/26/2012 6:37 AM, Michael Doukas wrote:
> "Nasser M. Abbasi" wrote in message <kbeqd7$r15$1@speranza.aioe.org>...
>> How did you get that columns 9 will have 56:67? I think it should be 52:62
>>
>>
> I am defining the ranges that each column may take.
> There is no need to be of the same length for my purposes, they just need to
>increment with a step of 1.
> I do not see any problem with that.
>

col1: 0 1 2
col2: 3 4 5 6
col3: 7 8 9 10 11
col4: 12 13 14 15 16 17
col5: 18 19 20 21 22 23 24
col6: 25 26 27 28 29 30 31 32
col7: 33 34 35 36 37 38 39 40 41
col8: 42 43 44 45 46 47 48 49 50 51
col9: 52 53 54 55 56 57 58 59 60 61 62

So, may be you can explain again, how last column should be 56:67?

--Nasser

Subject: Matrix variable with parametric elements

From: Michael Doukas

Date: 26 Dec, 2012 13:12:08

Message: 6 of 11

"Nasser M. Abbasi" wrote in message <kberft$tjo$1@speranza.aioe.org>...
> On 12/26/2012 6:37 AM, Michael Doukas wrote:
> > "Nasser M. Abbasi" wrote in message <kbeqd7$r15$1@speranza.aioe.org>...
> >> How did you get that columns 9 will have 56:67? I think it should be 52:62
> >>
> >>
> > I am defining the ranges that each column may take.
> > There is no need to be of the same length for my purposes, they just need to
> >increment with a step of 1.
> > I do not see any problem with that.
> >
>
> col1: 0 1 2
> col2: 3 4 5 6
> col3: 7 8 9 10 11
> col4: 12 13 14 15 16 17
> col5: 18 19 20 21 22 23 24
> col6: 25 26 27 28 29 30 31 32
> col7: 33 34 35 36 37 38 39 40 41
> col8: 42 43 44 45 46 47 48 49 50 51
> col9: 52 53 54 55 56 57 58 59 60 61 62
>
> So, may be you can explain again, how last column should be 56:67?
>
> --Nasser
>

I do not have the prerequisite that the col(i+1) contains col(i)+1 elements.
The ranges in my first post are just an example.
My problem specific ranges are:
col1: 0:2
col2: 3:4
col3: 5:7
col4: 8:11
col5: 12: 22
col6: 23:24
col7: 25:26
col8: 27:39
col9: 40:52

For each column one random integer must be selected.
Therefore, two possible rows of the resulting array (B) may be:
Brow1=[1,4,7,9,15,23,25,32,50]
Brow2=[0,3,5,8,14,23,26,29,42]

I hope that I have made myself more clear now.

Subject: Matrix variable with parametric elements

From: Roger Stafford

Date: 26 Dec, 2012 14:06:08

Message: 7 of 11

"Michael Doukas" wrote in message <kbet38$613$1@newscl01ah.mathworks.com>...
> I do not have the prerequisite that the col(i+1) contains col(i)+1 elements.
> The ranges in my first post are just an example.
> My problem specific ranges are:
> col1: 0:2
> col2: 3:4
> col3: 5:7
> col4: 8:11
> col5: 12: 22
> col6: 23:24
> col7: 25:26
> col8: 27:39
> col9: 40:52
>
> For each column one random integer must be selected.
> Therefore, two possible rows of the resulting array (B) may be:
> Brow1=[1,4,7,9,15,23,25,32,50]
> Brow2=[0,3,5,8,14,23,26,29,42]
- - - - - - - - - - -
  Let c be a row vector which gives the initial value of each column integer range together with a final element which is 1 greater than the end of the last range. (In your example it would be as below.)

 c = [0,3,5,8,12,23,25,27,40,53];
 n = length(c)-1;
 m = 40;
 M = floor(repmat(diff(c),m,1).*rand(m,n))+repmat(c(1:n),m,1);

M is your desired matrix.

Roger Stafford

Subject: Matrix variable with parametric elements

From: Michael Doukas

Date: 26 Dec, 2012 14:54:08

Message: 8 of 11

"Roger Stafford" wrote in message <kbf08g$g99$1@newscl01ah.mathworks.com>...
> - - - - - - - - - - -
> Let c be a row vector which gives the initial value of each column integer range together with a final element which is 1 greater than the end of the last range. (In your example it would be as below.)
>
> c = [0,3,5,8,12,23,25,27,40,53];
> n = length(c)-1;
> m = 40;
> M = floor(repmat(diff(c),m,1).*rand(m,n))+repmat(c(1:n),m,1);
>
> M is your desired matrix.
>
> Roger Stafford

Thank you very much!
This is exactly what I was looking for!

Regards,
Mike

Subject: Matrix variable with parametric elements

From: Nasser M. Abbasi

Date: 26 Dec, 2012 19:01:49

Message: 9 of 11

On 12/26/2012 7:12 AM, Michael Doukas wrote:
------------------------
>
> I do not have the prerequisite that the col(i+1) contains col(i)+1 elements.
> The ranges in my first post are just an example.
> My problem specific ranges are:
> col1: 0:2
> col2: 3:4
....
> col9: 40:52
-------------------------


But that is NOT what you wrote in your original message. Here is what
you wrote

------------------------
>>first column to take random integer values in the range 0:2 (i.e. integers
>>0 or 1 or 2), second column to take random integer values in the range
>>3:6 (i.e. 3 or 4 or 5 or 6),
>>...
>>ninth column to take random integer values within the range 56:67 (i.e. 56 or 57...67)
-----------------------

So, how did the second the second column suddenly change from 3:6 to now
becoming 3:4? and the 9th column which was supposed to be 56:67 now become

Are you solving 2 different problems at the same time?

Strange. Any way. Not important. Just wanted to point out that what
you are asking for now, is not what you wrote.

--Nasser

Subject: Matrix variable with parametric elements

From: Roger Stafford

Date: 26 Dec, 2012 21:27:08

Message: 10 of 11

"Nasser M. Abbasi" wrote in message <kbfhj7$2nr$1@speranza.aioe.org>...
> So, how did the second the second column suddenly change from 3:6 to now
> becoming 3:4? and the 9th column which was supposed to be 56:67 now become
>
> Are you solving 2 different problems at the same time?
>
> Strange. Any way. Not important. Just wanted to point out that what
> you are asking for now, is not what you wrote.
>
> --Nasser
- - - - - - - - - -
  Nasser, where Michael states that "the ranges in my first post are just an example", he is indicating that he wishes to have those ranges subject to change rather than having a fixed set of them. That is why I wrote the code with an adjustable vector c which determines the intervals. I think that is what he was hinting at by the word 'parametric' in the subject title.

  I'll have to confess that I too was initially mislead into assuming that last interval was 52:62 and had a vectorized code all ready to present to do just that. The confusion would have been avoided if he had stated the desired variability of the intervals in the first post.

Roger Stafford

Subject: Matrix variable with parametric elements

From: Nasser M. Abbasi

Date: 26 Dec, 2012 22:38:33

Message: 11 of 11

On 12/26/2012 3:27 PM, Roger Stafford wrote:
> "Nasser M. Abbasi" wrote in message <kbfhj7$2nr$1@speranza.aioe.org>...
>> So, how did the second the second column suddenly change from 3:6 to now
>> becoming 3:4? and the 9th column which was supposed to be 56:67 now become
>>
>> Are you solving 2 different problems at the same time?
>>
>> Strange. Any way. Not important. Just wanted to point out that what
>> you are asking for now, is not what you wrote.
>>
>> --Nasser
> - - - - - - - - - -


> Nasser, where Michael states that "the ranges in my first post are just an example",

But OP said that these are "just" an example, only later on, and _not_
the first time they asked the question. First time the question was
asked, it did not say anything that input can change and how it can change.
Here is the question again

--------------------------------------
I want to create a 40X9 matrix and store it as a variable, but
the matrix elements must be "parametric", meaning that i want the
elements of the:
first column to take random integer values in the range 0:2 (i.e. integers 0 or 1 or 2),
second column to take random integer values in the range 3:6 (i.e. 3 or 4 or 5 or 6),
...
ninth column to take random integer values within the range 56:67 (i.e. 56 or 57...67)
---------------------------------------------

I do not see anything in the above saying that the interval can change and how
it can change.

>he is indicating that he wishes to have those ranges subject to change rather than having a
>fixed set of them.

Yes. But that was later on.

> That is why I wrote the code with an adjustable vector c
>which determines the intervals. I think that is what he was hinting at by
>the word 'parametric' in the subject title.
>

You always had a good way to read into the minds of people who ask
questions here :)

I tend not to read minds and not second guess, but answer the question
as was asked.
  
> I'll have to confess that I too was initially mislead into assuming that
>last interval was 52:62 and had a vectorized code all ready to present to do just
>that. The confusion would have been avoided if he had stated the desired
>variability of the intervals in the first post.
>
> Roger Stafford
>

Yes. Ofcourse. having clear specification is always a good thing to do in science
and mathematics. Too bad that many questions asked are sloppy and with no
clear specifications and only after many tries one will start to find what
the person is actually asking for.

--Nasser

Tags for 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