ozlem
ozlem

Reputation: 73

Changing the values in a matrix in MATLAB

I have an UNBALANCED dataset containing five fields like:

a_code  b_code sector  year  value 
1        2      15     1970  1000   
2        3      16     1971  2900
3        2      15     1970  3900    

I want to create a 4-dimensional matrix in MATLAB for the "value" field. So I want to have a value field in a matrix such as M(a_code,b_code,sector,year) = value. I have 75 a_code, 75 b_code, 19 sectors and 45 years. So a NaN matrix is (75,75,19,45).

Since my dataset is not balanced (for example I don't have a value for a_code = 3, b_code = 1, sector = 15, year = 1970), I don't have a value for each (a_code, b_code, sector, year) combination. For the unavailable values, I want to have NaN. I know how to create a 4-dimensional matrix with NaN values, but how do I replace these NaN values with the ones in my dataset?

Probably I should write a loop, but I don't know how.

Upvotes: 0

Views: 2587

Answers (1)

eat
eat

Reputation: 7530

Here is some simple code to fulfill your requirements:

D= [1 2 15 1970 1000; 2 3 16 1971 2900; 3 2 15 1970 3900];
m= min(D(:, 1: end- 1))- 1;
shape= max(D(:, 1: end- 1))- m+ 1;

X= NaN(shape);
for k= 1: size(D, 1)
    n= D(k, 1: end- 1)- m;
    X(sub2ind(shape, n(1), n(2), n(3), n(4)))= D(k, end);
end
X(1, 1, 1, 1) %=> 1000
X(2, 2, 2, 2) %=> 2900
X(3, 1, 1, 1) %=> 3900

You may like to elaborate more on your specific situation, there may exists more suitable approaches. For example from your question, its not quite so clear why you need to have your data represented as a 4D array.

Upvotes: 1

Related Questions