Access to the elements with row number greater than the row number of element - matlab

I have a 3x3 array, called q, for example:
q=[1,2,3;4,5,6;7,8,9]
u=one of the element in array q
[row,col]=find(q==u) % to find the row and column number of element u
how to access the elements with row number greater than the row number of element u?
i really appreciate the help, if someone can share the ideas.

Related

given the 10x10 matrix m:(10 10)#100?1f In kdb/q

given the 10x10 matrix m:(10 10)#100?1
Find the average of every row and column of the matrix
Find the average of the matrix
Take the first element from the first row, the second from the second row etc
find the diagonal elements of a matrix
First I'm going to change the input so you can better see the solution. The 100?1 will give a list of 100 0s which I don't think is what you want. Maybe you wanted either 100 random (0;1) or 100 random values between 0-1. I went for the latter.
q)show m:(10 10)#100?1.
0.4655548 0.8455166 0.7281041 0.7403385 0.5199511 0.199172 0.9548708 0.498..
0.86544 0.3112134 0.3520122 0.4485896 0.6742543 0.2357538 0.7589261 0.318..
0.7053699 0.8153197 0.5051956 0.7546554 0.08613905 0.7824787 0.2080171 0.282..
So, now the questions.
Find the average of every row and column of the matrix.
q)meanRows:avg each m
q)meanCols:avg each flip m
UPDATE From Comment. You can get the average of a matrix column without using each or flip but will return null if any element is null. Another note is that if a column is length 10, 5 of which are null. Then the avg will only consider the average of the 5 non-null values.
So, if you believe there are nulls you may want to get rid of them and then get the mean values:
q)m:^[0;m] //Replace null with 0s if necessary
q)meanCols:avg m //Get avg without flipping or using each
Find the average of the matrix
q)avg avg each m
I think that^ is the quickest way to get the overall mean because it doesn't require razing or flipping.
Take the first element from the first row, the second from the second row etc
q)getVector:{[mtx]mtx'[c;c:til count mtx]}
q)getVector m
0.4655548 0.3112134 0.5051956 0.6333324 0.7258795 0.8671843 0.7556175 0.17954..
Let me know if you have any further questions.

Finding the max element in a column of a matrix excluding certain rows

Consider I have an nxn matrix. My goal is to find the max element of the first column, swap the row containing that largest element and the first row. Next, I want to find the max element of the second column, excluding the first row, then swap the row of this new max element and the second row. Again, finding the max element of the jth column excluding rows 1:j-1, then swapping the max element row with the jth row, up until the n-1th column (as during the nth column, I would only be able to choose from the nth row).
My current setup for this is as follows
for j = 1:n-1
[~,row]=max(A(j:n,j));
temp = A(row,:);
A(row,:)=A(j,:);
A(j,:)=temp;
...
While the switching function works well enough, [~, row]=max(A(j:n,j)) is able to, and consistently does for the matrix I'm specifically working on, output row 1 during the second iteration of j. My thought process behind this was that j:n represents the rows we want to check. Noting j=2, for its second iteration, I hoped this would search row 2-to-n; however, it seems to still check every row.
While this question has been asked before, the answer, I found, was this same line of code.
You are using [~,row]=max(A(j:n,j));. Let's say you are in iteration j=2. max will only consider the input from the 2nd row on. A return value of row=1 indicates a maximum in the second row of A. The first row you put into the function. The max function has no clue that you actually input something larger. You have to correct for this.
n=5
A=magic(n); %just some input data
for j = 1:n-1
[~,row]=max(A(j:n,j));
row=row+j-1;
temp = A(row,:);
A(row,:)=A(j,:);
A(j,:)=temp;
end
by the way, matlab can do row swapping without helper variable:
for j = 1:n-1
[~,row]=max(A(j:n,j));
row=row+j-1;
A([row,j],:)=A([j,row],:);
end

Find the largest value of each row in Matlab and divide each number in that row by the number

I have a 133120x4 matrix in Matlab.
I would like to find the largest value in each row, and divide every element in that row by that particular value.
Do I need to use some sort of loop? For example: I find the amount of rows in that matrix (133120), and iterate the loop that amount of times, then I go row by row and use the max function to return the largest value in that row, and divide each element in that row by the returned value from max.
Or is there a quicker way of doing this?
Thanks
EDIT (for clarification):
lets call my 133120x4 matrix A. I want to divide every element in a row by the largest value in that row. Since max and element-division are vectorized, would the solution simply be:
A_normal = A / max(A)
resulting in a 133120x4 matrix, but within each row, the largest value would be 1.
Is this correct? EDIT: It is not correct, and am still trying to figure out solution. Help from the community is greatly appreciated
Compute the maximum with max, repeat the result N(=4) times so there is one per each element and then element wise division
!
newMat=mat./repmat(max(mat,[],2),[1 size(mat,2)]);]
or in R2016b or newer just
newMat=mat./max(mat,[],2);

How can i change indices of a matrix in Matlab?

My problem is i want to assign some numbers to indices of a matrix. For example if I remove first row and first column of a matrix, then in remaining matrix 3th row and 4 column would actually be 4th row and 5th column in the first place.
I can do it with Array1(Array2) , however my code will have many seperate recursions so it is frustrating to keep track of everything. So, is there an once and for all way to map original 1..n indices to remaining matrix even after I remove rows and columnsth
Thanks in advance
You can do something like this as per beaker's suggestion
originalMatrix = magic(4)
dimension = size(originalMatrix)
indexMatrix = zeros(dimension(1), dimension(2))
for i = 1:numel(indexMatrix)
indexMatrix(i) = i
end
and remove the required row and column from indexMatrix.

Remove elements at a set of indices in a multidimensional array in MATLAB?

I have a multidimensional array A with 1000 elements (1000x3). I have another vector with index positions of elements I want to remove from this array.
I've tried using this A(indices) = [] or A(indices,:,:) = [], but the problem is that the result changes A's dimension, so if indices has 10 elements, I find A's size become 2990x1 instead of 990x3. Anyone can advise how to remove the elements having the indices in A where A's dimensions won't change will still be n x 3?
You can use logical indexing to filter the matrix, for example,
A=rand(1000,3);
A(A(:,1)>0.9)=[];
which removes the rows of A that have a value greater than 0.9 in the first column.
I'm not sure why your original approach didn't work though.