MATLAB Answers

0

How to select cell values and cell positions from a matrix?

Asked by Parthu P on 7 Nov 2019 at 2:32
Latest activity Commented on by Suleman ZP on 11 Nov 2019 at 3:43
Hi,
I have a set of six month daily data (attached) in matrix (A) (31 x 6 size).
I used [cell_value, cell_position] = min(A,[],1) to find minimum value (cell value) and corresponding cell position in each column of A. I got two metrices:
  1. cell_value (1 x 6);
  2. cell_position (1 x 6);
I want two more metrices, "new_cell_value" and "new_cell_positions" such that,
3. new_cell_value - should have all the values less than 1.5*cell_value (row size of this matrix is unknown but has 6 columns), and
4. new_cell_positions of new_cell_value (row size of this matrix is unknown but has 6 columns).
The data is attached. Please help me to solve this.
Thanks in advance.

  2 Comments

What have you tried so far?
Thank you for response.
I tried
[cell_value, cell_position] = min(A,[],1) to find minimum value (cell value) and corresponding cell position in each column of A - it worked very well
I then tried
B = find(A >== 1.15.*cell_value(:,6));
This doesn't give any error but it reads intended values incorrectly (in between A and 1.15.*cell_value). I need all values from A less than "1.15.*cell_value".

Sign in to comment.

1 Answer

Answer by Suleman ZP on 7 Nov 2019 at 7:10
Edited by Suleman ZP on 7 Nov 2019 at 7:12
 Accepted Answer

You find mininum cell value and position with this line of code.
[cell_value, cell_position] = min(A,[],1);
and its results 1 row with 6 columns like this.
cell_value =
90.8900 71.9900 136.0800 120.4500 115.6900 121.9400
cell_position =
13 1 22 25 28 29
Now using below code we can found all values less than 1.15.*cell_value in each columns.
new_cell_positions = zeros(size(A));
new_cell_values = zeros(size(A));
B = A < 1.15.*cell_value;
for i = 1:6
loca = find(B(:,i) == 1);
new_cell_positions(1:length(loca),i) = loca;
new_cell_values(1:length(loca),i) = A(loca,i);
end
this results in random number of rows but fix 6 columns.
new_cell_values =
91.5800 71.9900 154.8600 135.8900 124.0500 140.0400
90.8900 0 150.4500 132.4300 122.9100 138.4500
92.0800 0 153.3600 127.5200 116.3900 138.1300
99.4600 0 155.6300 123.2600 115.6900 137.9900
0 0 155.6900 120.4500 116.6400 135.8200
0 0 152.6400 120.9000 115.8600 137.0000
0 0 145.3900 121.3700 0 132.1700
0 0 151.4300 123.2700 0 127.9400
0 0 149.5000 123.8800 0 126.9800
0 0 136.6300 124.5200 0 127.9400
0 0 140.7500 0 0 129.0600
0 0 146.2000 0 0 131.1200
0 0 154.8700 0 0 129.5800
0 0 136.0800 0 0 126.6600
0 0 141.1900 0 0 125.1200
0 0 153.2900 0 0 121.9400
0 0 0 0 0 121.9700
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
new_cell_positions =
12 1 1 21 25 1
13 0 2 22 26 2
14 0 3 23 27 3
31 0 4 24 28 4
0 0 5 25 29 5
0 0 6 26 30 19
0 0 7 27 0 20
0 0 8 28 0 21
0 0 11 29 0 22
0 0 12 30 0 23
0 0 13 0 0 24
0 0 14 0 0 25
0 0 15 0 0 26
0 0 22 0 0 27
0 0 23 0 0 28
0 0 30 0 0 29
0 0 0 0 0 30
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

  4 Comments

Show 1 older comment
Well I am using MATLAB 2019a.
here I updated the code.
[cell_value, cell_position] = min(A,[],1);
new_cell_positions = zeros(size(A));
new_cell_values = zeros(size(A));
for i = 1:6
B = A(:,i) < 1.15.*cell_value(i);
loca = find(B == 1);
new_cell_positions(1:length(loca),i) = loca;
new_cell_values(1:length(loca),i) = A(loca,i);
end
Hi Suleman,
Thanks very much for response. I really appreciate your time and effort.
Unfortunately, updated code makes new_cell_positions, new_cell_values, and B all-zero.
Rather previous code worked well but I had difficulty with "B = A < 1.15.*cell_value " in putting right dimensions.
Please help me with dimensions next to "cell_value" in "B = A < 1.15.*cell_value;" in the previous code.
Thank you in advance.
Can you please share the out of this line. So I can compare and understant the issue. B = A < 1.15.*cell_value;

Sign in to comment.