How can I generate a matrix?

1 回表示 (過去 30 日間)
net
net 2013 年 11 月 20 日
コメント済み: net 2013 年 11 月 20 日
Hi, I have two array and I want to generate one matrix according to these arrays.
For example:
a={2,5,2,4,2,3,4,4,8}
b={1,3,1,3,4,2,3,3,2}
Matrix is 2 to 1 -> 1, 5 to 3 -> 1, 2 to 1 -> 2 (again) ...
Output:
Thanks for your helps.

採用された回答

Simon
Simon 2013 年 11 月 20 日
Hi!
Loop over your a/b vectors and increase the specified matrix element:
M = zeros(max(a), max(b));
for n = 1:length(a)
M(a(n), b(n)) = M(a(n), b(n)) + 1;
end
  1 件のコメント
net
net 2013 年 11 月 20 日
Thanks for your help.

サインインしてコメントする。

その他の回答 (2 件)

Laurent
Laurent 2013 年 11 月 20 日
a={2,5,2,4,2,3,4,4,8};
b={1,3,1,3,4,2,3,3,2};
yourmat=zeros(8,5);
for ii=1:length(a)
yourmat(a{ii},b{ii})=yourmat(a{ii},b{ii})+1;
end
Why are you using a cell array for a and b?
  2 件のコメント
net
net 2013 年 11 月 20 日
Array "a" is my states and "b" is my observations. I want to know that states and observations amount.
Sorry for my english.
net
net 2013 年 11 月 20 日
Yes you are right, I wrote with wrong parenthesis. Thanks.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2013 年 11 月 20 日
編集済み: Andrei Bobrov 2013 年 11 月 20 日
a=[2,5,2,4,2,3,4,4,8];
b=[1,3,1,3,4,2,3,3,2];
sz = [8 5];
out = nan(sz + 1);
out(2:end,2:end) = accumarray([a(:),b(:)],1,sz);
out(1,2:end) = 1:sz(2);
out(2:end,1) = 1:sz(1);
  2 件のコメント
Shani
Shani 2013 年 11 月 20 日
Andrei do you know about neural networks?
net
net 2013 年 11 月 20 日
Thanks for your help.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by