Matlab Array Formation From Data Set

1 view (last 30 days)
Calum
Calum on 25 Sep 2014
Answered: Guillaume on 24 Oct 2014
I have an array of data e.g:
A = [300 10 1; 100 20 2; 100 30 4; 200 30 5;]
I would like to compile the third column data from in a new array as follows:
B = [1 0 0; 0 2 0; 0 4 5;]
where the X and Y 'scales' of array B are known and match the values found in columns 1 and 2 of array A:
X = [300 100 200]
Y = [10 20 30]
Any help is much appreciated,
Regards,
CS
  2 Comments
dpb
dpb on 25 Sep 2014
Edited: dpb on 25 Sep 2014
1)
size(A)==[4,3] but size(B)==[3,3] ???
2)
Show/define the process by which you "scale" to get the values in B
I can make a reasonable fixup for 1) by presuming
A(3,:)=[];
to remove the third row. I can then make a first stab for 2) for
B(:,1)=fix(A(:,1)/X(1));
but I see no such similar relationship to the desired results shown for the other two columns...
Guillaume
Guillaume on 25 Sep 2014
Edited: Guillaume on 25 Sep 2014
What happens when two rows of A have got the same X and Y?

Sign in to comment.

Answers (2)

Michael Haderlein
Michael Haderlein on 25 Sep 2014
Edited: Michael Haderlein on 25 Sep 2014
Ha, got it:
X=[300 100 200];Y=[10 20 30];
A=[300 10 1;100 20 2;100 30 4;200 30 5];
[x,y]=meshgrid(X,Y)
[C,ia,ib]=intersect([x(:) y(:)],A(:,1:2),'rows')
B=zeros(length(Y),length(X));
B(ia)=A(ib,3)
B =
1 0 0
0 2 0
0 4 5
Maybe there's a shorter solution, but this one works fine.
  2 Comments
dpb
dpb on 25 Sep 2014
Very creative from the input!!! :)

Sign in to comment.


Guillaume
Guillaume on 24 Oct 2014
The solution I had in mind when I commented on the question was:
X=[300 100 200];Y=[10 20 30];
A=[300 10 1;100 20 2;100 30 4;200 30 5];
[~, col] = ismember(A(:, 1), X);
[~, row] = ismember(A(:, 2), Y);
B=zeros(numel(Y), numel(X));
B(sub2ind(size(B), row, col)) = A(:, 3);
However, my question still stands: 'What happens when two rows of A have got the same X and Y?' The above will simply use the latest value.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!