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:
Help with insertion

Subject: Help with insertion

From: Diego Zegarra

Date: 7 Apr, 2009 01:32:02

Message: 1 of 8

Hey guys I need help here,

sol1 = [3 7 2 9 5;4 6 1 10 8]

Assuming i1=1, and randomly selecting a number out of row i1 from sol1, (j1=7)

sol1(i1,:)=[3 7 2 9 5]

tam(i1)=length(sol1(i1,:));
j1=round(rand*(tam(i1)-1))+1;
x=sol1(i1,j1);

Now I want to get j1 and insert it in all different positions in that row,

Getting the following,
[7 3 2 9 5],
[3 7 2 9 5],
[3 2 7 9 5],
[3 2 9 7 5],
[3 2 9 5 7]

Any help will be much appreciated!

Subject: Help with insertion

From: Roger Stafford

Date: 7 Apr, 2009 02:25:04

Message: 2 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <greaei$ki5$1@fred.mathworks.com>...
> Hey guys I need help here,
>
> sol1 = [3 7 2 9 5;4 6 1 10 8]
>
> Assuming i1=1, and randomly selecting a number out of row i1 from sol1, (j1=7)
>
> sol1(i1,:)=[3 7 2 9 5]
>
> tam(i1)=length(sol1(i1,:));
> j1=round(rand*(tam(i1)-1))+1;
> x=sol1(i1,j1);
>
> Now I want to get j1 and insert it in all different positions in that row,
>
> Getting the following,
> [7 3 2 9 5],
> [3 7 2 9 5],
> [3 2 7 9 5],
> [3 2 9 7 5],
> [3 2 9 5 7]
>
> Any help will be much appreciated!

  I doubt if your random selection of j1 is what you really want. As it stands it will select each of the values, 1 and tam(i1), with half the probability of any of the other indices between these. For example, in your case with 5 elements in the row, you are doing a 'round' on 4*rand+1. For 'rand' between 0 and 1/8, it will round to 1. Between 1/8 and 3/8, it will round to 2. Between 3/8 and 5/8, to 3; between 5/8 and 7/8, to 5, and finally between 7/8 to 1, to 4. These aren't equal probabilities. For equal probabilities use

 j1=ceil*(tam(i1)*rand);

These will be uniformly distributed over the indices.

  As for the insertion, what are you inserting values into, five different row vectors, or a single 5 x 5 matrix? Either operation is very easy to do. What is your problem with that?

Roger Stafford

Subject: Help with insertion

From: Diego Zegarra

Date: 7 Apr, 2009 02:32:02

Message: 3 of 8

I am selecting the job j1 and want to insert it in every possible position of row i1 in sol1. So those 5 options are the ones I should come up with. How can I do that?

Thanks for the rand thing, you are right on that!

Subject: Help with insertion

From: Matt Fig

Date: 7 Apr, 2009 02:34:02

Message: 4 of 8

This is quick and easy to do with a little for loop. As Roger said, you are not randomly selecting an element with your formula (I correct Roger's error below.).


sol1 = [3 7 2 9 5;4 6 1 10 8];
i1 =1;
tam(i1) = length(sol1(i1,:));
j1 = ceil(tam(i1)*rand);
X = sol1(i1,j1);

for ii = tam(i1):-1:1
    v = 1:5;
    v(j1) = [];
    H{ii} = [sol1(i1,v(1:ii-1)),X,sol1(i1,v(ii:end))];
end

Subject: Help with insertion

From: Diego Zegarra

Date: 7 Apr, 2009 02:38:02

Message: 5 of 8

Into all different positions, by that I mean

[3 7 2 9 5]

Taking out j1 from the row and putting it before the 3, then between the 2 and the 9, then between the 9 and the 5 and lastly after the 5.

Hope you understood my explanation. It is a loop where I use the first result of insertion and do some calculations on that, then get the second result of the insertion and do some calculations and so on.

Subject: Help with insertion

From: Diego Zegarra

Date: 7 Apr, 2009 03:26:01

Message: 6 of 8

Thanks a lot Matt, that works like a charm. Now what if instead of just inserting that value into row i1 you do it to every position in every row,

So having this,

sol1 = [3 7 2 9 5;4 6 1 10 8];
sol=sol1;
i1 =1;
tam(i1) = length(sol1(i1,:));
j1 = ceil(tam(i1)*rand);
X = sol1(i1,j1);

I want the following solutions assuming I select j1=7,
These are the same as the ones before,
[7 3 2 9 5;4 6 1 10 8];
[3 7 2 9 5;4 6 1 10 8];
[3 2 7 9 5;4 6 1 10 8];
[3 2 9 7 5;4 6 1 10 8];
[3 2 9 5 7;4 6 1 10 8];

These are the new ones,
[3 2 9 5 0 0;7 4 6 1 10 8];
[3 2 9 5 0 0;4 7 6 1 10 8];
[3 2 9 5 0 0;4 6 7 1 10 8];
[3 2 9 5 0 0;4 6 1 7 10 8];
[3 2 9 5 0 0;4 6 1 10 7 8];
[3 2 9 5 0 0;4 6 1 10 8 7];

Thanks a lot!

Subject: Help with insertion

From: Matt Fig

Date: 7 Apr, 2009 03:42:01

Message: 7 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message
> Thanks a lot Matt, that works like a charm. Now what if instead....

> more requests ... It works, but did he understand it?

> Thanks a lot!




I really think that what you have been given is so easily modified to do what you want that it might be insulting for me to do it for you.

Good luck.

Subject: Help with insertion

From: Bruno Luong

Date: 7 Apr, 2009 12:48:01

Message: 8 of 8

One of many solutions, but this one is vectorized

% Use this package
% http://www.mathworks.com/matlabcentral/fileexchange/23391

sol1=[3 7 2 9 5;
         4 6 1 10 8]

% Peak an element
i1=1; j1=2;
peak=sol1(i1,j1);
% Select a row, 2nd in this case
v=sol1(2,:);

% Engine
n=size(v,2);
V1=repmat(v,[n 1]);
V2=zeros(n+1);
V2(itriu(size(V2),+1))=V1(itriu(size(V1)));
V2(itril(size(V2),-1))=V1(itril(size(V1)));
V2(idiag(size(V2)))=peak;

disp(V2)

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