I have a structure A with fields B and C.
Values of B are double and can take on the value of 1 or 2.
Values of C are double and range in value from 1 to about 50.
Both B and C have the same number of elements (when a value is assigned to C then a 1 or 2 is assigned to B).
The following code is supposed to find all index values where B equals 1 and then use those index values to find the minimum value of C.
>> a=find(A(1).B(:)==1);
>> [value,index]=min(A(1).C(a))
value = 5.020000000000000
index = 630
As you can see below, when I put the minimum value index back into B it returns a 2.
>> A(1).B(630)
ans = 2
The problem stems from the fact that vector a is not the same size as original vector A(1).B(:). So the value you get in index refers to the corresponding location in a not in A(1).C(:) to get the correct index use:
A(1).B(a(index))
Related
I'm new to Matlab and I'm just stuck with this line of code:
[r,c] = find(abs(fh) <= 2 );
Beware: ironically it was easy for me understanding what the right part of the assignment is.
The left part however (which is maybe the definition of a variable)... I don't know how to search because I have too generic results by googling just square brackets with something inside.
My assumption is this line of code is creating some a matrix with r rows and c columns but r and c are nowhere to be found in the rest of the code.... or maybe it's a simple array with two elements... but it doesn't make much sense to me honestly.
Can you guys help me please?
Whenever you see that syntax, it means that the function being called is returning more than one output argument (two in this case).
The best way to learn about the function output arguments is to check the documentation:
https://www.mathworks.com/help/matlab/ref/find.html#d120e368337
[row,col] = find(___) returns the row and column subscripts of each
nonzero element in array X using any of the input arguments in
previous syntaxes.
The output arguments are positional, so r is row, c is col.
Take a look in Matlab find() docs.
If X is a vector, then find returns a vector with the same orientation as X.
If X is a multidimensional array, then find returns a column vector of the linear indices of the result.
If X contains no nonzero elements or is empty, then find returns an empty array.
If you call
X = [18 3 1 11;
8 10 11 3;
9 14 6 1;
4 3 15 21 ]
[row,col] = find(X>0 & X<10,3)
You will get:
row = 3×1
2
3
4
col = 3×1
1
1
1
Which represents the index (row number and column number) of each elements that satifies the condition you defined. Since it returns more than 1 value, you can divide the output in two different variables and that is what the left side represents.
I am trying with following code, but get indice of last value only
A=[ 3 4 1 2 4 4 4]
B=unique(A)
[b1 b2]=max(B)
while i<=numel(A)
if A(i)==A(b2)
ID=A(i)
end
end
Is there any way other in matlab to get all indices of value 4 (max value).
If you want to find the indices of the largest value in your matrix, there is no need for unique at all. It's superfluous. Just use find and max simultaneously:
ID = find(A == max(A));
max(A) returns the largest value in A. A == max(A) returns a logical vector where 1 corresponds to a value in A matching the largest value in A and 0 otherwise. Finally, find determines the locations in the input that are non-zero, so in effect we are finding the locations that match the largest value in A.
This doesn't make any sense to me and I'm not sure what to even search for.
Matlab code:
[a b] = max(.9);
Output:
a =
0.9
b =
1
Why is it increasing by 1/10? What does [a b] do when on the left side of equal sign?
max is used to find the maximum value of an array. The second output (if requested), returns the index that corresponds to the first value in the array that is equal to the maximum value.
[max_value, max_index] = max([1 3 3 2]);
% max_value = 3
% max_index = 2
In your case, you are passing a scalar (a 1 x 1 array) to max, therefore the scalar is the maximum and the maximum appears at index 1.
[M,I] = max(A) finds the indices of the maximum values of A and returns them in output vector I, using any of the input arguments in the previous syntaxes. If the maximum value occurs more than once, then max returns the index corresponding to the first occurrence.
Reference: https://www.mathworks.com/help/matlab/ref/max.html?requestedDomain=www.mathworks.com
I am new to matlab and I was wondering what it meant to use logical indexing/masking to extract data from a matrix.
I am trying to write a function that accepts a matrix and a user-inputted value to compute and display the total number of values in column 2 of the matrix that match with the user input.
The function itself should have no return value and will be called on later in another loop.
But besides all that hubbub, someone suggested that I use logical indexing/masking in this situation but never told me exactly what it was or how I could use it in my particular situation.
EDIT: since you updated the question, I am updating this answer a little.
Logical indexing is explained really well in this and this. In general, I doubt, if I can do a better job, given available time. However, I would try to connect your problem and logical indexing.
Lets declare an array A which has 2 columns. First column is index (as 1,2,3,...) and second column is its corresponding value, a random number.
A(:,1)=1:10;
A(:,2)=randi(5,[10 1]); //declares a 10x1 array and puts it into second column of A
userInputtedValue=3; //self-explanatory
You want to check what values in second column of A are equal to 3. Imagine as if you are making a query and MATLAB is giving you binary response, YES (1) or NO (0).
q=A(:,2)==3 //the query, what values in second column of A equal 3?
Now, for the indices where answer is YES, you want to extract the numbers in the first column of A. Then do some processing.
values=A(q,2); //only those elements will be extracted: 1. which lie in the
//second column of A AND where q takes value 1.
Now, if you want to count total number of values, just do:
numValues=length(values);
I hope now logical indexing is clear to you. However, do read the Mathworks posts which I have mentioned earlier.
I over simplified the code, and wrote more code than required in order to explain things. It can be achieved in a single-liner:
sum(mat(:,2)==userInputtedValue)
I'll give you an example that may illustrate what logical indexing is about:
array = [1 2 3 0 4 2];
array > 2
ans: [0 0 1 0 1 0]
using logical indexing you could filter elements that fullfil a certain condition
array(array>2) will give: [3 4]
you could also perform alterations to only those elements:
array(array>2) = 100;
array(array<=2) = 0;
will result in "array" equal to
[0 0 100 0 100 0]
Logical indexing means to have a logical / Boolean matrix that is the same size as the matrix that you are considering. You would use this as input into the matrix you're considering, and any locations that are true would be part of the output. Any locations that are false are not part of the output. To perform logical indexing, you would need to use logical / Boolean operators or conditions to facilitate the selection of elements in your matrix.
Let's concentrate on vectors as it's the easiest to deal with. Let's say we had the following vector:
>> A = 1:9
A =
1 2 3 4 5 6 7 8 9
Let's say I wanted to retrieve all values that are 5 or more. The logical condition for this would be A >= 5. We want to retrieve all values in A that are greater than or equal to 5. Therefore, if we did A >= 5, we get a logical vector which tells us which values in A satisfy the above condition:
>> A >= 5
ans =
0 0 0 0 1 1 1 1 1
This certainly tells us where in A the condition is satisfied. The last step would be to use this as input into A:
>> B = A(A >= 5)
B =
5 6 7 8 9
Cool! As you can see, there isn't a need for a for loop to help us select out elements that satisfy a condition. Let's go a step further. What if I want to find all even values of A? This would mean that if we divide by 2, the remainder would be zero, or mod(A,2) == 0. Let's extract out those elements:
>> C = A(mod(A,2) == 0)
C =
2 4 6 8
Nice! So let's go back to your question. Given your matrix A, let's extract out column 2.
>> col = A(:,2)
Now, we want to check to see if any of column #2 is equal to a certain value. Well we can generate a logical indexing array for that. Let's try with the value of 3:
>> ind = col == 3;
Now you'll have a logical vector that tells you which locations are equal to 3. If you want to determine how many are equal to 3, you just have to sum up the values:
>> s = sum(ind);
That's it! s contains how many values were equal to 3. Now, if you wanted to write a function that only displayed how many values were equal to some user defined input and displayed this event, you can do something like this:
function checkVal(A, val)
disp(sum(A(:,2) == val));
end
Quite simply, we extract the second column of A and see how many values are equal to val. This produces a logical array, and we simply sum up how many 1s there are. This would give you the total number of elements that are equal to val.
Troy Haskin pointed you to a very nice link that talks about logical indexing in more detail: http://www.mathworks.com/help/matlab/math/matrix-indexing.html?refresh=true#bq7eg38. Read that for more details on how to master logical indexing.
Good luck!
%% M is your Matrix
M = randi(10,4)
%% Val is the value that you are seeking to find
Val = 6
%% Col is the value of the matrix column that you wish to find it in
Col = 2
%% r is a vector that has zeros in all positions except when the Matrix value equals the user input it equals 1
r = M(:,Col)==Val
%% We can now sum all the non-zero values in r to get the number of matches
n = sum(r)
M =
4 2 2 5
3 6 7 1
4 4 1 6
5 8 7 8
Val =
6
Col =
2
r =
0
1
0
0
n =
1
Suppose
>> hhh=sparse([],[],[],2^40,1);
>> hhh(7)=1;
>> a=hhh(7)
Please, note that hhh(7)>0 and 1>0 have a large time difference in computation times.
How can I get only the value of hhh(7) to the variable a without the sparse data structure?
Example
Input: assignment with the number and the sparse data structure, wrong.
a =
(1,1) 1
Goal: assignment just with a number
>> a=1
a =
1
Use full to convert your partial matrix (matrix element), e.g. the element to be stored in a, to mat-type:
a = full(hhh(7))
Note that this will also work for other selections, such as
a = full(hhh(7:9))
The output would then be
a =
1
0
0
When you assign an expression to a variable, you will "inherit" all the properties of the expression (where possible). To prevent this, you need to assign to an element of a previously declared object. For example:
a = 0;
a(1) = hhh(7);
Now you should have
>> a
a =
1