Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions

DW Simpson
Actuarial Jobs

Visit our site for the most up to date jobs for actuaries.

Actuarial Salary Surveys
Property & Casualty, Health, Life, Pension and Non-Tradtional Jobs.

Actuarial Meeting Schedule
Browse this year's meetings and which recruiters will attend.

Contact DW Simpson
Have a question?
Let's talk.
You'll be glad you did.


Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-01-2018, 05:00 PM
CuriousGeorge CuriousGeorge is offline
Member
CAS SOA
 
Join Date: Dec 2005
Posts: 1,207
Default 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..
Reply With Quote
  #2  
Old 07-03-2018, 10:49 PM
Fish Actuary's Avatar
Fish Actuary Fish Actuary is offline
Member
Non-Actuary
 
Join Date: Jun 2006
Location: Australia
Posts: 11,001
Default

Quote:
Originally Posted by CuriousGeorge View Post
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.
Reply With Quote
  #3  
Old 07-04-2018, 07:26 AM
The_Polymath's Avatar
The_Polymath The_Polymath is offline
Member
CAS SOA
 
Join Date: Jun 2016
Posts: 502
Default

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..
Reply With Quote
  #4  
Old 07-05-2018, 11:14 AM
CuriousGeorge CuriousGeorge is offline
Member
CAS SOA
 
Join Date: Dec 2005
Posts: 1,207
Default

Quote:
Originally Posted by The_Polymath View Post
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.
Reply With Quote
  #5  
Old 07-13-2018, 05:34 PM
Philip_Trick Philip_Trick is offline
CAS AAA
 
Join Date: Feb 2013
College: Boston University Alumni
Posts: 10
Default 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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 02:58 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.16347 seconds with 9 queries