0

I have a problem : suppose I have a matrix

A =

    -1     2    -3
    -4     5    -6
    -7     8    -9

I convert it into a column matrix

B = A(:)

B =

    -1
    -4
    -7
     2
     5
     8
    -3
    -6
    -9  

Suppose I want to force the first column elements to lie within a particular range (-range1 : range1) , second column elements within (-range2 : range2) & third column elements within (-range3:range3). I tried doing that by implementing this code :

range1 = 0;
range2 = -5;
range3 = 0;

B(B(1:3,1)<range1)=10;
B(B(4:6,1)>range2)=0;
B(B(7:9,1)<range3)=20;

The answer I get is this :

B =

    20
    20
    20
     2
     5
     8
    -3
    -6
    -9

Whereas the correct answer I should get is this :

B =

    10
    10
    10
     0
     0
     0
    20
    20
    20

What I am doing wrong ? Please help.

roni
  • 1,443
  • 3
  • 28
  • 49
  • Please check this link : http://stackoverflow.com/questions/18201729/forcing-the-columns-of-a-matrix-within-different-limits/18201856#18201856. It's a continuation of this question. – roni Aug 18 '13 at 06:49
  • Are you sure if your actual B output is correct? with the conditions you have mentioned – Zero Aug 18 '13 at 06:56
  • sorry I corrected the answer that I expect. Please check my edited question. – roni Aug 18 '13 at 07:01
  • Your expected outcome does not match the description of the problem at all. For example, `10` is not in the range of `-0:0` – Dennis Jaheruddin Aug 19 '13 at 08:35
  • I made a mistake. Should not have termed it as range. Please recheck my code! If the particular no's is greater than the no range1 or less than range1 then I am forcing it to 10 (a particular value). – roni Aug 19 '13 at 08:39

2 Answers2

1

What is wrong:

Look closely at your command:

>> B( B(7:9, 1) < range3 ) = 20;

And now let's do it step by step

You are conditioning on the last three elements B( 7:9, 1 ) which are -3, -6 and -9.
Therefore you end up with

>> B(7:9, 1) < range3
ans = 
  true
  true 
  true

You have a logical indexing of three elements. Using these logical indices to access B, which has 9 elements result with an access to the first three elements out of nine.
Thus, all your commands only modifes the first three elements of B without affecting the rest of B.

A possible fix:

You can actively define the range you are working on, for example, the second column:

>> aRange = 4:6;
>> B( aRange( B(aRange, 1) > range2 ) ) = 0

See how the three-vector logical indexing B(aRange, 1) > range2 ) now index aRange (which has 3 elements) and not B (which has 9 elements) directly.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thanks for the answer. I understood that too but could you tell me how I can modify my code such that correct elements of the matrix are getting accessed. I mean should I pad zeros or something to another matrix and then apply this indexing operation ? – roni Aug 18 '13 at 07:07
  • Yes I did see your edit now. Thanks for the answer. It works perfectly. – roni Aug 18 '13 at 07:10
  • Hi again. Can this be performed using the bsxfun function? Question continued from : http://stackoverflow.com/questions/18201729/forcing-the-columns-of-a-matrix-within-different-limits – roni Aug 19 '13 at 05:37
  • since you already "flatten" `B` into a vecotr, I don't think so. – Shai Aug 19 '13 at 06:14
1

You can do this:

A=[-1,2,-3;-4,5,-6;-7,8,-9];
range1 = 0;range2 = -5;range3 = 0;
B=A;
B((B(:,1)<range1),1)=10;
B((B(:,2)>range2),2)=0;
B((B(:,3)<range3),3)=20;

Output B is in mxn dimension as your A.

If you want it as column vector.

B=B(:);
Zero
  • 74,117
  • 18
  • 147
  • 154
  • Thanks this works too. But as I stated before I want the operation to do on the matrix B and not on the matrix A. The column matrix has already been formed. – roni Aug 18 '13 at 07:19
  • Well, just in case if you want to use mxn matrix you can ignore last line. And, the operation is done on matrix B only ;) – Zero Aug 18 '13 at 07:23
  • Hi again. Can this be performed using the bsxfun function? Question continued from : http://stackoverflow.com/questions/18201729/forcing-the-columns-of-a-matrix-within-different-limits – roni Aug 19 '13 at 05:37