Actuarial Outpost matlab question
 Register Blogs Wiki FAQ Calendar Search Today's Posts Mark Forums Read
 FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions

 Enter your email to subscribe to DW Simpson weekly actuarial job updates. li.signup { display: block; text-align: center; text-size: .8; padding: 0px; margin: 8px; float: left; } Entry Level Casualty Health Life Pension All Jobs

#1
07-01-2018, 05:00 PM
 CuriousGeorge Member CAS SOA Join Date: Dec 2005 Posts: 1,160
matlab question

I'm pretty new to matlab and I'm trying to figure out how to do this:

b=zeros(5,10)
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

a=(1;3;1;4;4);

For each row in b, I want to put a 1 in the column indexed by a, i.e.:
1 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0

edit: I've gotten it to work with a for loop, but this seems like it shouldn't be necessary.

Last edited by CuriousGeorge; 07-01-2018 at 05:05 PM..
#2
07-03-2018, 10:49 PM
 Fish Actuary Member Non-Actuary Join Date: Jun 2006 Location: Australia Posts: 10,682

Quote:
 Originally Posted by CuriousGeorge I'm pretty new to matlab and I'm trying to figure out how to do this: b=zeros(5,10) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a=(1;3;1;4;4); For each row in b, I want to put a 1 in the column indexed by a, i.e.: 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 edit: I've gotten it to work with a for loop, but this seems like it shouldn't be necessary.
It's been awhile since I used Matlab, but it looks to me like your notation is wrong for a.
#3
07-04-2018, 07:26 AM
 The_Polymath Member CAS SOA Join Date: Jun 2016 Posts: 373

Fish is correct.

You have created a column vector and are trying to use it
to index a product square matrix of the same size.

Last edited by The_Polymath; 07-04-2018 at 07:31 AM..
#4
07-05-2018, 11:14 AM
 CuriousGeorge Member CAS SOA Join Date: Dec 2005 Posts: 1,160

Quote:
 Originally Posted by The_Polymath Fish is correct. You have created a column vector and are trying to use it to index a product square matrix of the same size.
Yes. I do have a mx1 column vector, and I am trying to use it to populate a 1 at that index of a mxn matrix.

I think this is the looping code that did what I wanted:

for i=1:m
b(i,a(i))=1;
end

It worked, but it just seemed very inelegant, and I thought there must be a better way.
#5
07-13-2018, 05:34 PM
 Philip_Trick CAS AAA Join Date: Feb 2013 College: Boston University Alumni Posts: 9
Solve with Linear Indexing

The Matlab matrix can also be iterated into via linearly so, in your example example, the point b(2,3) is equivalent to b(12).

So, you need to figure out which row you're on and which column you're in and calculate that linear order point.

Because Matlab goes column-by-column for this linearly, the math problem you've got to solve is row + column-length * column minus 1.

So, your five points are the same as..
b(1,1) = b(1 + 0*5).
b(2,3) = b(2 + 2*5)
b(3,1) = b(3 + 0*5)
b(4,4) = b(4 + 3*5)
b(5,4) = b(5 + 3*5)

To turn this into MATLAB script you would write:

a = (1;3;1;4;4); % Your original lookup matrix.
a_mod = (1:5)' + (a-1) .* 5; % Converted based on columns of length 5
b(a_mod) = 1; % set each point equal to 1.

If you wanted to make this a bit more generic you could change it to:

a_mod = (1:size(a,1))' + (a-1).*size(b,1);

That would allow you to use a = [1;4;2;4] to edit the first 4 rows of b with a without throwing an error, but this'll only work if a is shorter than b.