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
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
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

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by