フィルターのクリア

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 Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by