MATLAB: Automatic assigning of matrix element indices - matlab

I am currently in the process of writing a custom function to compute the RREF of a given m x n matrix. Since I am a complete newbie to MATLAB, I thought it would be a good idea to sample the built-in rref() function.
While examining the part of code that found "the value and index of largest element in the remainder" of the leading column, I had that:
[p,k] = max(abs(A(i:m,j)))
where m is the number of rows of the matrix, and i=j=1.
I understand that max(abs(A(i:m,j))) gives you the value of the largest element in the leading column - a single scalar answer. However, I cannot understand why it manages to assign two values to [p,k], with kbeing the index number for p. could someone please be kind enough to help?

k is the position in your vector where the maximum value is.
For instance, assume we use the vector [1,2,5,2,1]. There the max value is 5. This value is at the third position in the vector. So [p,k] = max([1,2,5,2,1]);will return p=5 and k=3.
The function will assing values depending on how you call it.
p = max(...
will assign only p
[p,k] = max(...
will assign p and k.

Related

Reform matrix from a 2D logical matrix (without reshaping)

I tried to rebuild the matrix from the logical arguments as in the example below:
a=rand(2,5)
b=rand(2,5)
c=a>b
a(:,c)=b(:,c)
However I get Index exceeds matrix dimension error. Can this be done without reshaping the matrix beforehand?
If you want to copy the least value between, a and b into a for each entry try:
a=rand(2,5);
b=rand(2,5);
c = find(a>b); % c contains the position of value of the greatest val
a(c)=b(c) % removes greates value copying a smaller value from b
If you want to copy the greatest value modify the statement creating variable c.
Index exceeds matrix dimensions is caused by you trying to use as matrix dimensions a list : and a matrix c. Find creates a list of values which satisfy the relationship wanted and can be used to recall specific values.

Returning indices of variables in a three-dimensional matrix satisfying a condition

I am trying to find the value of 3 variables that satisfy a condition, namely that their sum is less than or equal to 1. My approach is to use ndgrid to sweep over all combinations of the variables and define a matrix I that contains a 1 if the condition is satisfied. My code is below
ss=0.25;
[pp1,pp2,pp3] = ndgrid(0:ss:1,0:ss:1,0:ss:1);
I = pp1+pp2+pp3<=1
My question is, how do I generate a list of all valid variable combinations? I wish to have a 3 x n vector p that contains all n valid values for pp1,pp2, and pp3.
I've found a solution which I'll post as an answer. It is simply
p = [pp1(I) pp2(I) pp3(I)]
If anyone has a better solution I would appreciate any comments.

Matlab Histogram Function

I'm new to Matlab and for an assignment my professor is having the class write (complete really) a custom Matlab function for generating a histogram from a set of data. Essentially a new vector is being created, L which is being updated with the information from a 2D matrix M. The first column of L contains the information from M(i,j) and in a second column contains the count (total) of M(i,j) in the data set. I'm in need of some direction as to how to proceed next.
Below is where I'm at thus far:
function L = hist_count(M)
L = [ [0:255' zeros(256,1) ];
for i = 1:size(M,1)
for j = 1:size(M,2)
L(double(M(i,j))+1,2) = <<finish code here>>;
end
end
figure;
plot(L(:1),L(:2));
The <<finish code here>> section is where I'm stuck. I understand everything up to the point where I need to update L with the information.
Assistance is appreciated.
Note: Your initialization of your histogram L has the brackets mismatched.
Remove the second [ bracket in the code. In addition, the creation of the 0:255 vector is incorrect. Doing 0:255' transposes the single constant of 255, which means that it will still create a horizontal vector of 0:255 which will make the code fail. You should surround the creation of this vector with parantheses, then transpose that result. Therefore:
L = [ (0:255)' zeros(256,1) ];
Now onto your actual problem. Judging by your initialization of the histogram, there are 256 possible values so your input is most likely of type uint8, which means that the values in your data will only be from [0-255] in steps of 1. Recall that a histogram records the total number of times you see a value. In this case, you have a two column matrix where the first column tells you the value you want to examine and the second column tells you how many times you see that value in your data. Therefore, each row tells you which value you are examining in your data as well as how many times you have seen that value in your data. Note that the counts are all initialized to zero, so the logic is that every time you see a value, you need to access the right row corresponding to the data point, then increment that value by 1.
Therefore, the line is simply just accessing the current count and adding 1 to it... you then store it back:
L(double(M(i,j))+1,2) = L(double(M(i,j))+1,2) + 1;
M(i,j) is the value found at location (i,j) in your 2D data. The last question you have is why cast the intensity to double and add 1? You cast to double because the input may be an integer type. This means that any values that are beyond the dynamic range of the type will get saturated. Because your input is type uint8, any values beyond 255 will saturate to 255. In MATLAB, we index into rows and columns of a matrix starting at 1 and because the values will potentially start at value 0, this corresponds to row 1 of your histogram so you have to offset by 1. When we get to the most extreme case of value 255 for type uint8 for example, adding 1 to this using the native uint8 will saturate to 255, which means that the values of 254 and 255 get lumped into the same bin. Therefore, you must convert to some type that extends beyond the limits of uint8 and then you add by 1 to avoid saturation. double is usually done here as a default as it has higher precision than uint8, but any type that is higher than uint8 in precision is suitable.

Assigning the different row to another matrix after comparing two matrices

i have two matrices
r=10,000x2
q=10,000x2
i have to find out those rows of q which are one value or both values(as it is a two column matrix) different then r and allocate them in another matrix, right now i am trying this.i cannot use isequal because i want to know those rows
which are not equal this code gives me the individual elements not the complete rows different
can anyone help please
if r(:,:)~=q(:,:)
IN= find(registeredPts(:,:)~=q(:,:))
end
You can probably do this using ismember. Is this what you want? Here you get the values from q in rows that are different from r.
q=[1,2;3,4;5,6]
r=[1,2;3,5;5,6]
x = q(sum(ismember(q,r),2) < 2,:)
x =
3 4
What this do:
ismember creates an array with 1's in the positions where q == r, and 0 in the remaining positions. sum(.., 2) takes the column sum of each of these rows. If the sum is less than 2, that row is included in the new array.
Update
If the values might differ some due to floating point arithmetic, check out ismemberf from the file exchange. I haven't tested it myself, but it looks good.

MATLAB: What's [Y,I]=max(AS,[],2);?

I just started matlab and need to finish this program really fast, so I don't have time to go through all the tutorials.
can someone familiar with it please explain what the following statement is doing.
[Y,I]=max(AS,[],2);
The [] between AS and 2 is what's mostly confusing me. And is the max value getting assigned to both Y and I ?
According to the reference manual,
C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.
[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned.
I think [] is there just to distinguish itself from max(A,B).
C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.
Also, the [C, I] = max(...) form gives you the maximum values in C, and their indices (i.e. locations) in I.
Why don't you try an example, like this? Type it into MATLAB and see what you get. It should make things much easier to see.
m = [[1;6;2] [5;8;0] [9;3;5]]
max(m,[],2)
AS is matrix.
This will return the largest elements of AS in its 2nd dimension (i.e. its columns)
This function is taking AS and producing the maximum value along the second dimension of AS. It returns the max value 'Y' and the index of it 'I'.
note the apparent wrinkle in the matlab convention; there are a number of builtin functions which have signature like:
xs = sum(x,dim)
which works 'along' the dimension dim. max and min are the oddbal exceptions:
xm = max(x,dim); %this is probably a silent semantical error!
xm = max(x,[],dim); %this is probably what you want
I sometimes wish matlab had a binary max and a collapsing max, instead of shoving them into the same function...