0

I am trying to understand what happens in MATLAB for this certain line of code

good_mintratio=handles.mintratio(handles.good)

handles.good is an array with size (48x60) where all the elements are 1. handles.mintratio is also an array with size (48x60) where it contains floats.

I ran the following simple code to understand what it exactly does but I am not quite sure what is really happening. For this piece of code,

A = [1,2,3; 4,5,6];
B = [1,1,1; 1,1,1];
C=A(B);

This returns C = [1,1,1;1,1,1]

It seems like C is like B but contains the elements of A where the elements of B is the indexes for A.

A = [1,2,3; 4,5,6];
B = [2,1,1; 1,5,1];
C=A(B);

Like, this will make C = [4,1,1;1,3,1]

But good_mintratio=handles.mintratio(handles.good) this forms a colmun with size (2880,1). It seems like all the elements got combined in to a single colmun but not sure why the size changed. Also, if handles.good has some 0's (which means false in MATLAB, right?), would that change the result?

color_blue
  • 31
  • 6
  • 1
    `handles.good` is a logical array maybe? Do `B = logical([1,1,1; 1,1,1]);` to replicate that situation. – Cris Luengo Apr 01 '21 at 05:07
  • Oh thank you so much! This was what I was looking for! So, when I make B an logical array, it seems like it collects the elements of A if the indexes are 1 (true). Do you have an idea how to do this in python? – color_blue Apr 01 '21 at 05:16
  • 1
    NumPy does the same thing if you index with a logical array. – Cris Luengo Apr 01 '21 at 05:19

1 Answers1

0

When you use indexing by arrays (As in C = A(B);) there are 2 options:

  1. The array B is based on non logical numbers (Double usually): The output is based on the linear indexing of the array A.
  2. The array B is based on logical elements: The output is a vector of the elements where true was in B.

For example:

clear();

mA = reshape(1:9, 3, 3);
mB = randi([1, 9], 3, 3);
mC = logical(randi([0, 1], 3, 3));

mA(mB)
mA(mC)

Then the output:

ans =

     5     9     5
     9     8     9
     8     7     4


ans =

     2
     5
     8
Royi
  • 4,640
  • 6
  • 46
  • 64