Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following two lists:

List A:

A={{-0.0390625, 0., 0., 0., 0.0390625}, {-0.174377, -0.073478, 
  3.0893*10^-16, 0.073478, 
  0.174377}, {-0.196939, -0.112309, -1.50053*10^-16, 0.112309, 
  0.196939}, {-0.174377, -0.073478, 5.52991*10^-16, 0.073478, 
  0.174377}, {-0.0390625, 0., 0., 0., 0.0390625}}

List B:

B={{5, 6, 6, 5, 5}, {5, 5, 1, 5, 5}, {5, 4, 3, 4, 5}, {5, 5, 5, 5, 
  5}, {5, 0, 5,6, 5}}

I want to create 6 (max. value in list B) copies of List A, but if a value at point {i,j} in List B is smaller then 6, by example value X at point {i,j} in List B, then the value at point {i,j} in List A should only be used in the first X copies. Hence the last 6-X copie(s) should be zero at point {i,j}. How can I fix this?

share|improve this question
    
If you show us your code so far, then we can try to fix it. –  Yves Klett Oct 8 '13 at 14:37
add comment

3 Answers 3

Try this (for convenience, I use smaller matrices)

n = 3;
max = 4;
A = RandomReal[1.0, {n, n}];
(B = RandomInteger[{1, max}, {n, n}]) // MatrixForm

enter image description here

res = A UnitStep[B - #] & /@ Range@Max[B];
Column[MatrixForm /@ res]

enter image description here

share|improve this answer
add comment

ybeltukov's solution rules. Here's a different way to get the same result:

res = Table[
   A Function[val, If[val <= i, 0, 1], Listable][B],
   {i, Range[max] - 1}
   ];
Column[MatrixForm /@ res]
share|improve this answer
add comment

Does this meet your requirements:

result=Transpose /@ 
 MapThread[{#1, #2, #3, #4, #5} &, 
  Transpose /@ MapThread[PadRight[Table[#1, {#2}], 6] &, {A, B}, 2], 
  2]

Visualizing:

Column[MatrixForm /@result]

yields: enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.