Matlab Array Formation From Data Set

1 次查看(过去 30 天)
Calum
Calum 2014-9-25
回答: Guillaume 2014-10-24
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 个评论
dpb
dpb 2014-9-25
编辑:dpb 2014-9-25
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 2014-9-25
编辑:Guillaume 2014-9-25
What happens when two rows of A have got the same X and Y?

请先登录,再进行评论。

回答(2 个)

Michael Haderlein
Michael Haderlein 2014-9-25
编辑:Michael Haderlein 2014-9-25
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.

Guillaume
Guillaume 2014-10-24
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by