Matrix combining with array in a specific manner

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 7 Giu 2012
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 Commenti
Ahsan Khan
Ahsan Khan il 7 Giu 2012
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 il 7 Giu 2012
Choose other directory for the 'Result.xls' (non 'C:\Program Files\MATLAB\R2012a\bin\')

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by