1

I have two matlab arrays, very large, over 41k rows each with 10 columns.

I also have an array of the exact same size filled with 1's and 0's. I need to apply this logic array to the first array and if the value is logic true, pass the number, otherwise if false it must return NaN.

using something like:

output= number(array)

Only gives an output of the positive logic array values but I need to keep the array the same size/structure, how can I do this?

Filburt
  • 16,951
  • 12
  • 63
  • 111
AMcNall
  • 519
  • 1
  • 4
  • 23

2 Answers2

2

First let us generate a dummy matrix and a dummy mask

A = rand(5,3);
M = randi([0 1], 5, 3);

then you can apply the mask to the original matrix

A(not(M)) = nan;
Jommy
  • 982
  • 1
  • 6
  • 13
  • 1
    The only issue here is that you lose information from the original `A`. This might be desirable though if memory is running low... Also `not(M)` is normally written as `~M` in Matlab – Dan Sep 09 '14 at 13:22
  • 2
    [`M==0 might be better than ~M or not(M)`](http://stackoverflow.com/questions/25339215/is-a-0-really-better-than-a) – Divakar Sep 09 '14 at 14:21
1

Pre-allocate output with NaNs:

output = NaN(size(number))
output(array) = number(array)
Dan
  • 44,224
  • 16
  • 81
  • 148
  • 1
    Though not tested, `output(1:size(number,1),1:size(number,2))= NaN` might be better with performance - http://undocumentedmatlab.com/blog/preallocation-performance – Divakar Sep 09 '14 at 14:26