3
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ If you show us your code so far, then we can try to fix it. $\endgroup$ – Yves Klett Oct 8 '13 at 14:37
2
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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]
$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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