how to add some value to random matrix with specific condition?

if i have A represent the number of group ones in matrix in the row
A = [ 1 2 0
2 1 1
3 1 1 ]
i want to generate matrix like this depend on A which between the group of ones at least one zero
F = [1 0 1 1 0 0 0
1 1 0 1 0 1 0
1 1 1 0 1 0 1 ]

Respuestas (1)

A = [ 1 2 0
2 1 1
3 1 1 ]
%
N = max(sum(1+A,2));
C = arrayfun(@(n){[ones(1,n),0]},A);
C = cellfun(@(c){[c{:}]},num2cell(C,2));
C = cellfun(@(v){[v,zeros(1,N-numel(v))]},C);
F = vertcat(C{:});
F(:,end) = []
prints the output in the command window:
F =
1 0 1 1 0 0 0
1 1 0 1 0 1 0
1 1 1 0 1 0 1

4 comentarios

when i apply this on
A = [ 1 0 0
3 0 0
5 0 0
7 0 0
9 0 0
11 0 0
13 0 0
13 0 0
15 0 0
15 0 0
15 0 0
6 1 6
4 1 4
3 0 0
5 0 0 ]
the solution will be
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0 0
1 1 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
and this is wrong where the solution will be
[ 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 0 1 1 1 1 1 1
0 1 1 1 1 0 0 1 0 0 1 1 1 1 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ]
Stephen23
Stephen23 el 29 de Abr. de 2016
Editada: Stephen23 el 29 de Abr. de 2016
My answer is not wrong, because you actually changed the requirements afterwards.
Please explain your new requirements clearly:
  1. exactly how many leading zeros there need to be, or
  2. where the ones should be.
and then explain how this new requirement matches your original example:
1 0 1 1 0 0 0
1 1 0 1 0 1 0
1 1 1 0 1 0 1
Why are these ones not centered ?
i want to write another question and i will explain what i need clearly
If you start another question, then you still should accept Stephen's answer considering he did answer the current question correctly.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Abr. de 2016

Comentada:

el 29 de Abr. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by