Actuarial Outpost matlab question
 User Name Remember Me? Password
 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

 DW SimpsonActuarial JobsVisit our site for the most up to date jobs for actuaries. Actuarial Salary SurveysProperty & Casualty, Health, Life, Pension and Non-Tradtional Jobs. Actuarial Meeting ScheduleBrowse this year's meetings and which recruiters will attend. Contact DW SimpsonHave a question? Let's talk. You'll be glad you did.

 Thread Tools Search this Thread Display Modes
#1
07-01-2018, 05:00 PM
 CuriousGeorge Member CAS SOA Join Date: Dec 2005 Posts: 1,207
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: 11,001

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: 502

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,207

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: 10
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.

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off

All times are GMT -4. The time now is 03:17 AM.

 -- Default Style - Fluid Width ---- Default Style - Fixed Width ---- Old Default Style ---- Easy on the eyes ---- Smooth Darkness ---- Chestnut ---- Apple-ish Style ---- If Apples were blue ---- If Apples were green ---- If Apples were purple ---- Halloween 2007 ---- B&W ---- Halloween ---- AO Christmas Theme ---- Turkey Day Theme ---- AO 2007 beta ---- 4th Of July Contact Us - Actuarial Outpost - Archive - Privacy Statement - Top