Matlab Array Formation From Data Set

1 vue (au cours des 30 derniers jours)
Calum
Calum le 25 Sep 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 commentaires
dpb
dpb le 25 Sep 2014
Modifié(e) : dpb le 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 le 25 Sep 2014
Modifié(e) : Guillaume le 25 Sep 2014
What happens when two rows of A have got the same X and Y?

Connectez-vous pour commenter.

Réponses (2)

Michael Haderlein
Michael Haderlein le 25 Sep 2014
Modifié(e) : Michael Haderlein le 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 commentaires
dpb
dpb le 25 Sep 2014
Very creative from the input!!! :)
Calum
Calum le 24 Oct 2014
Perfect! Thanks :)

Connectez-vous pour commenter.


Guillaume
Guillaume le 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.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by