필터 지우기
필터 지우기

Matrix combining with array in a specific manner

조회 수: 1 (최근 30일)
Ahsan Khan
Ahsan Khan 2012년 6월 7일
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에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by