Matrix combining with array in a specific manner

2 次查看(过去 30 天)
Hello MATLABERS,
how do i combine a array of integers with a Matrix of Integers. ex: Matrix A = [1 2 3 4 5 6 7 8 9 10] is combined with a matrix B = [2 5 7;1 2 9 ;4 5 6] in such a way that the resultant matrix C results should be as follows: so if a sequence has a one in it, it is stored right under one==>
[1 2 3 4 5 6 7 8 9 10;0 2 0 0 5 0 7 0 0 0;1 2 0 0 0 0 0 0 9 ;0 0 0 4 5 6 0 0 0]
1 2 3 4 5 6 7 8 9 10
0 2 0 0 5 0 7 0 0 0
1 2 0 0 0 0 0 0 9 0
0 0 0 4 5 6 0 0 0
P.s if ur interested in only a scalar combined with a matrix a quick solution was posted by Wayne King http://www.mathworks.com/matlabcentral/answers/40436-matrix-filling

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-6-7
i1 = arrayfun(@(ii)ismember(A,B(ii,:)),(1:size(B,1)),'un',0);
out = ones(size(B,1),1)*A.*cat(1,i1{:});
or
out = bsxfun(@times,cat(1,i1{:}),A)
This is a replay of question
  2 个评论
Ahsan Khan
Ahsan Khan 2012-6-7
thanks a MIL...works like a charm. but now that im trying to store the reults to a excel file im getting an error:
xlswrite('Result.xls',out);
the error im getting is...
Error using xlswrite (line 220)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Microsoft Excel cannot access the
file 'C:\Program Files\MATLAB\R2012a\bin\'.
There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the
same name as a currently open workbook.
Help File: xlmain11.chm
Help Context ID: 0
Error in Prac (line 8)
xlswrite('Result.xls',out)
Andrei Bobrov
Andrei Bobrov 2012-6-7
Choose other directory for the 'Result.xls' (non 'C:\Program Files\MATLAB\R2012a\bin\')

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by