MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Noru
on 3 Apr 2013

I have a hard case (in my opinion), and i need your favor for this thing..

I have a data like below :

data = [1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0];

And 2 matrix like below :

A =

11 3 17 1 5 19 6 18 6 16 18 7 11 15 14 13 4 2 20 4

B =

4.0078 1.9147 4.9880 1.8967 1.7225 1.1802 1.1200 3.1427 2.6425 4.9374 2.4181 4.8850 3.6921 2.9100 3.0406 4.6255 3.5859 4.3326 2.4275 3.6506

I want to change the value in matrix 'data' row 2 with matrix B, the changes according to the matrix A. Matrix A use as a number that will synchronize with matrix 'data' row 1. For example, let see the first line of matrix A and matrix B. In matrix 'data' row 2 the value in line 11 and 3 (elements of matrix A) will change from 0 to 4.0078 and 1.9147 (elements of matrix B), so the matrix data will change like below :

data = [1 0 2 0 3 1.9147 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 4.0078 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 ];

The changes will looping per line of matrix A and B, so the results will generates 10 different matrix data combination. I already tried to coding it, but there’s still wrong and I stuck with it. My coding shown below :

A = zeros(10,2); B = zeros(10,2);

for C = 1:10,

d = 20; a = randperm(d); a = a(1:2); A(C,:) = a;

b = (1+(5-1).*rand(1,2)); B(C,:) = b;

end

for C = 1:10 for i1 = 1:size(a), for i2 = 1:size(b), index = a(i1); if data(index, 2) == 0 data(index, 2) = b(i2); end end end end

Did anyone had the solution..?

Thank you..

*No products are associated with this question.*

Answer by Andrei Bobrov
on 5 Apr 2013

Edited by Andrei Bobrov
on 7 Apr 2013

Accepted answer

sa = size(A,1); out = accumarray([A(:), repmat((1:sa)',2,1)],B(:),[size(data,1) sa]);

or

sa = size(A,1); sd = size(data,1); out = zeros(sd,sa); out(sub2ind([sd,sa],A,repmat(1:sa,2,1)')) = B;

ADD

sd = size(data,1); data = reshape(num2cell(permute(cat(3,repmat((1:sd)',1,sa),out),... [1 3 2]),[1 2]),[],1);

Noru
on 6 Apr 2013

thank you for your help, your solution real close to my expectation but i want to separate each combination of matrix 'data' with loop operation, so there are 10 matrix 'data' and not merged into one matrix 'data'..

how to do that..?

thank you..

## 2 Comments

## Matt Kindig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69647#comment_140918

How do you handle the case where A has repeated indices, but B does not have repeated values? For example, in your shown data, the A(4,1) and A(5,1) elements both =4, but B(4,1) ~= B(5,1).

## Noru (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69647#comment_141332

in my case above there's no problem with that, because line 4 and line 5 will applied in different matrix 'data'.

the problem is how to insert elements of matrix A and B per line to matrix 'data' so it will create 10 different matrix 'data' combination. one line for one matrix 'data'.

do you have any solutions..?