0

I have a 3D matrix n x m x p where p represents 1400 daily 2D data arrays. I also have a logical array the length of p x 1.

How do I create a new 3D matrix that contain only the 'pages' where p=1?

lisse
  • 1
  • 3

2 Answers2

2

This should work in the same way as logical indexing for 1D or 2D arrays.

n = 3; m = 4; p = 5;
tmp = rand([n,m,p]);
p_mask = logical([1 0 1 0 0]);
new = tmp(:,:,p_mask);

A small caveat is, that the "logical" array has to be of the logical type. If you use a double array with ones and zeros, Matlab will assume that you want to do regular (positional) indexing and complain because you're trying to access the element at the index 0 which is out-of-bounds:

Index in position 3 is invalid. Array indices must be positive integers or logical values.

Edit: The cast to the logical type above just serves to illustrate the point about "logical" indexing only working with the logical type, and not with floating point or integer types. Usually, the logical indices would instead be defined by some condition like for example p_mask = p_vals == 1; where p_vals would be the array containing ones and zeros. It's of course also not necessary to create a new variable for the logical mask - new = tmp(:,:,p_vals == 1) would also do just fine.

-3

In MATLAB pulling pages or slices off this n*m*p matrix is as simple as choosing what pages to take.

np=1400
n=3;m=3;                   % page size, just as example

A=randi([-20 20],n,m,np);  % the n x m x p input matrix

p=randi([0 1],1,np);       % selector

p_selection=find(p==1);

B=A(:,:,p_selection);      % selected pages
John BG
  • 690
  • 4
  • 12
  • 1
    You’re showing the same thing as the other answer, but with the unnecessary and wasteful `find` added. – Cris Luengo Jun 09 '23 at 17:05
  • I am not using the unnecessary command logical to work indices. Calling a command wasteful is ridiculous. In this context command 'find' is ok. – John BG Jun 12 '23 at 17:03
  • actually command find is a really useful command, let's have a positive attitude, shan't we? – John BG Jun 12 '23 at 17:05
  • 2
    If you do `p_selection = p==1` you get exactly the same end result. `find` implies a computation overhead, depending on the number of elements found it can be quite significant. MathWorks recommends not using it in the way you use it here. Sorry for my earlier comment being short and to-the-point, that often comes across as rude, I did not mean to be rude. I’m here to help people learn. – Cris Luengo Jun 12 '23 at 17:36
  • Then Mr/Ms Luengo, if you seriously think you have a better answer I kindly suggest you post it along the already available answers. You see, there may or may be not a unique answer to this question. It may also be the case that a question, not necessarily this one, may have no right or wrong answer. The answer may also be trivial. But among all possibilities, it's up to the question originator to choose the one that best assists in their endeavours. Obviously a moderator with enough points may or may not pull in and have a say too. So why don't we just let Lisse decide? – John BG Jun 12 '23 at 17:46
  • 3
    So instead of “thank you, I didn’t know that”, you go the “I can’t be wrong, don’t give me facts” route. Good to know. —— I hope my comment is at least useful to OP and future readers, I hope someone learns something from it. – Cris Luengo Jun 12 '23 at 19:34