Matrix operation and manipulation
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Hi guys,
I am working on a problem that needs to identify all values in every coulmn of a matrix that are less than zero. How can I implement this?
A=rand(200,30)
C=size(x,30) % returns the matrix with all values in A that are less than 0
%%
I used the following and it returned a single column and I cannot identify the values from which they come from
C=A(A<0);
Any idea will be appreciated,
8 Comments
the cyclist
on 21 May 2020
What exactly would you want the output from the following array to be?
A = [ -1 0 -2;
0 -3 0;
-4 -5 -6]
Please define the output you would want using MATLAB syntax, not just words.
Lewis S
on 21 May 2020
Stephen23
on 21 May 2020
And what about this?:
A = [ -1 5 -2;
0 -3 0;
-4 -5 -6]
John D'Errico
on 21 May 2020
The problem is, what if different columns of that matrix had a differnt number of values that satisfy the goal? For example:
X = rand(5)
X =
0.0340 0.6317 0.9451 0.8636 0.6303
0.8858 0.7000 0.5990 0.1550 0.6053
0.2288 0.8886 0.8334 0.1229 0.4067
0.3141 0.4946 0.8537 0.9254 0.9245
0.6003 0.7972 0.3876 0.5557 0.8117
>> X < 0.5
ans =
5×5 logical array
1 0 0 0 0
0 0 0 1 0
1 0 0 1 1
1 1 0 0 0
0 0 1 0 0
Now I cannot reduce X to a new RECTANGULAR matrix that has only the elements of X in each column that are greater than 1/2 (or less than 1/2, for that matter) since then we would have a matrix with a different number of elements per column.
You could create a cell array with those results. But that would be a more complicated endeavor, and would force you to learn to work with cell arrays.
Lewis S
on 21 May 2020
the cyclist
on 21 May 2020
Again, just describing with words is not that helpful. It leaves too much room for misunderstanding. It is impossible to know what you mean by "cluster the columns". So, for this input matrix:
A = [ -1 5 2;
0 -3 0;
-4 -5 -6]
(which cannot be reduced to a rectangular output), what exactly do you want for the output?
Lewis S
on 21 May 2020
John D'Errico
on 21 May 2020
Edited: John D'Errico
on 21 May 2020
This is not an answer, since the question seems to be diverging. And don't make up syntax that you hope might work. That will never work out. Instead, learn to use the tools of MATLAB.
In the matrix X, we saw that you can create a logical array, indicating the elements of a matrix X that satisfy a goal. I'll change it a little so that only some columns would be located. There are two columns with an element that does not exceed 0.2 in that matrix.
X < 0.2
ans =
5×5 logical array
1 0 0 0 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 0
0 0 0 0 0
Now, what information do you want to see? A list of the column indices that have a true result there? What can you do? I can think of at least a few ways to achieve that.
Could we try this?
[rind,cind] = find(X<0.2)
rind =
1
2
3
cind =
1
4
4
Now, what does this tell you?
unique(cind)
ans =
1
4
So we have a simple way to get a list of all columns in X, such that at least one element was less than 0.2. This first solution actually requires two lines of code to achieve the result you asked for.
Another way, that can be done in one line of code:
any(X<0.2 , 1)
ans =
1×5 logical array
1 0 0 1 0
What does that tell you? READ THE HELP FOR ANY. Does it get us closer to you target? Now we can do this:
find(any(X<0.2 , 1))
ans =
1 4
When you get stuck on a problem in MATLAB, look for tools that can do what you want. When you don't see any direct, obvious solution, look for tools that will get you close to what you are looking for. Can you then take another step to get to where you want to go?
In both of the schemes I show here, I did exactly that. I tried to apply one tool to the problem, then worked with the result of that tool to reach the goal.
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!