function to calculate summation in matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
if i have a (M x N) matrix_A with zeros and another (M x N/2) matrix_B filled by number and combine these matrices to each other which the number in the matrix_B represent numbers of 1 in the matrix_A .. For Example
matrix_A = [0 0 0 0 0 0 0 0 0 0 ] ...... 10 of zeros
matrix_B = [1 2 2 2]
the solution must be in a first matrix after apply the function .. for the previous example the solution is
matrix_A = [1 0 1 1 0 1 1 0 1 1]
Note : in matrix_B between each of number of one's there must be a zero or more .
the equation is if N == ∑k(i) + (n-1) then the function will be apply . where ∑k(i) = 1+2+2+2 = 7 , (n-1) is the number of numbers in the row in matrix_B (4 -1) = 3 .. then when we apply the equation .. 10 == 7+3 ------ true then apply the function.
댓글 수: 2
Azzi Abdelmalek
2016년 3월 31일
What is your question? to make it clear, post a short example, you don't need to post tens of numbers, just a small matrix, with expected result, explaining how to obtain it, sometimes, the expected result is enough to explain what you want.
답변 (1개)
Roger Stafford
2016년 4월 1일
Instead of inserting ones into a pre-existing A of zeros, it is easier to generate A from scratch:
A = [];
for k = 1:size(B,2)
A = [A,ones(1,B(k)),0];
end
A = A(1:end-1); % Remove the extra zero
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!