Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
inserting conditional data in a multidimensional matrix without using nested loop

Subject: inserting conditional data in a multidimensional matrix without using nested loop

From: Tapos

Date: 9 Sep, 2010 13:06:24

Message: 1 of 6

hi,
how can i extract and change data from a multi-dimensional matrix?
i can create a matrix containing all zeros or ones using v=ones(8,8,8); then i need to modify some of the elements using conditional statement depending upon the position of the elements.
can anybody help me?

here is the code for the matrix with for loop and conditions:

<pre lang="MATLAB">
for Z=1:8
    for Y=1:8
        for X=1:8
            if ((Z==1) | (Z==8))
                V(X,Y,Z)=0; % top and bottom layers
            elseif (.1)^2 < ( ((X-4.5)*.01)^2 + ((Y-4.5)*.01)^2 )
                V(X,Y,Z)=0; % outside the tank
            elseif ((.03)^2 < (((X-4.5)*.01+0)^2+((Y-4.5)*.01+0)^2)) | ((Z-.4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4)
                V(X,Y,Z)=1; % outside the sample
            else
                V(X,Y,Z)=2; % inside the sample
            end
        end % x loop
    end % y loop
end % z loop
</pre>

thanks

Subject: inserting conditional data in a multidimensional matrix without using nested loop

From: Oleg Komarov

Date: 9 Sep, 2010 13:19:21

Message: 2 of 6

"Tapos " <tapos1008@gmail.com> wrote in message <i6am4g$7s7$1@fred.mathworks.com>...
> hi,
> how can i extract and change data from a multi-dimensional matrix?
> i can create a matrix containing all zeros or ones using v=ones(8,8,8); then i need to modify some of the elements using conditional statement depending upon the position of the elements.
> can anybody help me?
>
> here is the code for the matrix with for loop and conditions:
>
Use logical indexing (search in the documentation);

Example:
% Create random matrix
A = rand(8,8,8);

% Change values between 0.5 and 0.8 to NaN
A(A >= .5 & A <= .8) = NaN;

Place the conditions inside the brackets and the assignment on the right.

Post any problems/errors

Oleg

Subject: inserting conditional data in a multidimensional matrix without using nested loop

From: Roger Stafford

Date: 9 Sep, 2010 15:57:05

Message: 3 of 6

"Tapos " <tapos1008@gmail.com> wrote in message <i6am4g$7s7$1@fred.mathworks.com>...
> hi,
> how can i extract and change data from a multi-dimensional matrix?
> i can create a matrix containing all zeros or ones using v=ones(8,8,8); then i need to modify some of the elements using conditional statement depending upon the position of the elements.
> can anybody help me?
>
> here is the code for the matrix with for loop and conditions:
>
> <pre lang="MATLAB">
> for Z=1:8
> for Y=1:8
> for X=1:8
> if ((Z==1) | (Z==8))
> V(X,Y,Z)=0; % top and bottom layers
> elseif (.1)^2 < ( ((X-4.5)*.01)^2 + ((Y-4.5)*.01)^2 )
> V(X,Y,Z)=0; % outside the tank
> elseif ((.03)^2 < (((X-4.5)*.01+0)^2+((Y-4.5)*.01+0)^2)) | ((Z-.4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4)
> V(X,Y,Z)=1; % outside the sample
> else
> V(X,Y,Z)=2; % inside the sample
> end
> end % x loop
> end % y loop
> end % z loop
> </pre>
>
> thanks
- - - - - - - - -
 [X,Y,Z] = ndgrid(1:8,1:8,1:8);
 p = Z==1 | Z==8 | (.1)^2 < ( ((X-4.5)*.01).^2 + ((Y-4.5)*.01).^2 );
 q = ((.03)^2 < (((X-4.5)*.01+0).^2+((Y-4.5)*.01+0).^2)) | ...
      ((Z-.4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4);
 V = (~p) + (~(p|q));

Roger Stafford

Subject: inserting conditional data in a multidimensional matrix without using nested loop

From: Roger Stafford

Date: 9 Sep, 2010 16:17:04

Message: 4 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6b04h$35a$1@fred.mathworks.com>...
> [X,Y,Z] = ndgrid(1:8,1:8,1:8);
> p = Z==1 | Z==8 | (.1)^2 < ( ((X-4.5)*.01).^2 + ((Y-4.5)*.01).^2 );
> q = ((.03)^2 < (((X-4.5)*.01+0).^2+((Y-4.5)*.01+0).^2)) | ...
> ((Z-.4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4);
> V = (~p) + (~(p|q));
>
> Roger Stafford
- - - - - - -
  There's a typo in the q line. It should read:

 q = ((.03)^2 < (((X-4.5)*.01+0).^2+((Y-4.5)*.01+0).^2)) | ...
      ((Z-4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4);

Roger Stafford

Subject: inserting conditional data in a multidimensional matrix without using nested loop

From: Tapos

Date: 9 Sep, 2010 16:23:05

Message: 5 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6b1a0$hnh$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6b04h$35a$1@fred.mathworks.com>...
> > [X,Y,Z] = ndgrid(1:8,1:8,1:8);
> > p = Z==1 | Z==8 | (.1)^2 < ( ((X-4.5)*.01).^2 + ((Y-4.5)*.01).^2 );
> > q = ((.03)^2 < (((X-4.5)*.01+0).^2+((Y-4.5)*.01+0).^2)) | ...
> > ((Z-.4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4);
> > V = (~p) + (~(p|q));
> >
> > Roger Stafford
> - - - - - - -
> There's a typo in the q line. It should read:
>
> q = ((.03)^2 < (((X-4.5)*.01+0).^2+((Y-4.5)*.01+0).^2)) | ...
> ((Z-4.5)*.01 > .4) | ((Z-4.5)*.01 < -.4);
>
> Roger Stafford
***********************************
marvellous! exactly what i am looking for!!
many many thanks Roger.
now trying to understand the code... :)

Subject: inserting conditional data in a multidimensional matrix without using nested loop

From: Roger Stafford

Date: 9 Sep, 2010 17:56:05

Message: 6 of 6

"Tapos " <tapos1008@gmail.com> wrote in message <i6b1l9$ap5$1@fred.mathworks.com>...
> .......
> now trying to understand the code... :)
- - - - - - - - -
  The explanation is not difficult. The expressions ~p and ~(p|q) are logical expressions that are each either true or false at the various points. When you subject them to the arithmetic operation in (~p) + (~(p|q)), the false cases are converted to numerical zeros and the true cases to numerical ones before the addition is carried out. Therefore V is set a) to zero when both are false, b) to one when ~p is true but ~(p|q) is false, and c) to two when both are true. These three cases correspond respectively to the cases a) when p is true, b) when p is false and q is true, and c) when p and q are both false, which is what your for-loops do.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us