필터 지우기
필터 지우기

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.
  댓글 수: 2
dpb
dpb 2014년 9월 25일
Very creative from the input!!! :)
Calum
Calum 2014년 10월 24일
Perfect! Thanks :)

댓글을 달려면 로그인하십시오.


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