Variable Matrix and replace values according to set criteria

16 views (last 30 days)
Hello
I have an issue and dont know the exact way to approach it
I want to replace the number of a column based on the values of two preceding columns and change the number it has to something else.
The matrix i have generated has 4 columns always but the number or rows is extremely big, Basically I have created a file for the use in unstructured grids, with 1st column representing node number(which may be infinite) the two subsequent columns are x-coordinate and y -coordinate, the last column is the so called flag identifier which initially is either 1 or 0
#nodes x y flag
1 -11 60 0
2 -11 61 1
3 -10 61 1
4 -11 60.5 2
...etc
What i want to do is, to set a criterion that will instruct based on a standard x-value coordinateand a variable y-value coordinate and replace the flag (4th column with another number either 2,3, 4).
Often i want to have a steady value of x criterion and detect all the correspoding coordinates and change the final column.
for example i want to have x=-11 and then any y coordinate from y=60 to 61 has to be detected and the flag column to be changed into 2, so the new format of my file will be
1 -11 60 2
2 -11 61 2
3 -10 61 1
4 -11 60.5 2
I thought to use one approach I had in one of my trials, when i wanted to visuallize and separate my grid, but i dont think this will work now and somehow use IS function
d(:,4)==0;
% find which # nodes have the 0 or 1 as last column and classify it into a table A
A=find(d(:,4)==0);
% create a table that will include only the values that had 0 or 1 at their erd column and give me in the two other columns the values % d1=new table
% d= original table
% A=table with #nodes containing the nodes with 0
% 1:2= give the values for the nodes with 0 and give columns 1&2
d_0=d(A,1:2:3:4); %
Any help and suggestions please??
kind regards
George

Answers (2)

Jos (10584)
Jos (10584) on 7 Mar 2014
logical indexing will help you
d = [1 -11 60 0 ; 2 -11 61 1 ; 3 -10 61 1 ; 4 -11 60.5 2]
tf = d(:,2) == -11 & (d(:,3)>=60 | d(:,3)<=61)
d(tf,4) = 2
  1 Comment
George
George on 7 Mar 2014
Edited: George on 7 Mar 2014
I only get a logical table but the main file has no value changed
1 0 0 0
2 0 0 0
3 -7.00911600000000 58.1850830000000 1
4 -7.00911600000000 58.1856700000000 1
5 -7.00979900000000 58.1854100000000 0
6 -7.00999600000000 58.1846430000000 1
7 -7.00999700000000 58.1862570000000 0
8 -7.01263700000000 58.1824430000000 1
9 -7.01322300000000 58.1821500000000 0
10 -7.01087700000000 58.1842030000000 0
so the column (4) is the problem but to change that its imperative i rely on a double criterion
a constant value of x and a variable of y (and/or vice verca)
thank you

Sign in to comment.


Iain
Iain on 7 Mar 2014
matrix = [# x y f];
matrix(matrix(:,2) > 4,4) = 4; % This sets all the flags, where x is greater than 4, to 4.
matrix(matrix(:,2) > 4 & matrix(:,2) < 5,4) = 4; % This sets all the flags, where x is greater than 4 and x is less than 5, to 4.
matrix(matrix(:,3) > 4 & matrix(:,2) < 5,4) = 4; % This sets all the flags, where y is greater than 4 and x is less than 5, to 4.
Hopefully you get the idea ;)
  1 Comment
George
George on 7 Mar 2014
Edited: George on 7 Mar 2014
I tried the third option but it does not allow for a (= )
d(d(:,3) = 58.1 & d(:,2) > -7.00,4) = 2;
|
Error: The expression to the left of the equals sign is not a valid target for
an assignment.
1 0 0 0
2 0 0 0
3 -7.00911600000000 58.1850830000000 1
4 -7.00911600000000 58.1856700000000 1
5 -7.00979900000000 58.1854100000000 0
6 -7.00999600000000 58.1846430000000 1
7 -7.00999700000000 58.1862570000000 0
8 -7.01263700000000 58.1824430000000 1
9 -7.01322300000000 58.1821500000000 0
10 -7.01087700000000 58.1842030000000 0 so the column (4) is the problem but to change that its imperative i rely on a double criterion
a constant value of x and a variable of y (and/or vice verca)
thank you

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!