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

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);

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.

How do I save the indices of the top 4 maximum numbers in a matrix in scilab

I need to save the indices of 4 maximum numbers
for example, I need to get the indices of rows 10,9,7,5
5.0259327
4.7127487
4.8435524
4.8538644
5.1048996
6.2441973
5.9413803
6.2912638
5.1117512
5.8309519
5.7419509
6.9663477
5.9958319
6.9519781
6.5802736
6.7327558
7.6765878
I have used
[mA,nA]=max(distA)
where mA is the row and nA is the column
in getting one maximum number but I cannot figure out how to choose another maximum number without duplication. I cannot sort because I need the indices.
You can use the gsort function:
[S,ind]=gsort(distA,"g","d");
The index of the 4 largest elements is the given by
ind(1:4)
You can use this little trick.
[output_val, output_index] = max(input_mat(input_mat < max(input_mat)))
This will give you the value and index of the second largest element. And then similarly, you can do it for 4 numbers.

MATLAB Extracting Column Number

My goal is to create a random, 20 by 5 array of integers, sort them by increasing order from top to bottom and from left to right, and then calculate the mean in each of the resulting 20 rows. This gives me a 1 by 20 array of the means. I then have to find the column whose mean is closest to 0. Here is my code so far:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
MeanArray= mean(transpose(NewArray(:,:)))
X=min(abs(x-0))
How can I store the column number whose mean is closest to 0 into a variable? I'm only about a month into coding so this probably seems like a very simple problem. Thanks
You're almost there. All you need is a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
ColNum = find(abs(mean(NewArray,1))==min(abs(mean(NewArray,1)))); %// gives you the column number of the minimum
MeanColumn = RandomArray(:,ColNum);
find will give you the index of the entry where abs(mean(NewArray)), i.e. the absolute values of the mean per column equals the minimum of that same array, thus the index where the mean of the column is closest to 0.
Note that you don't need your MeanArray, as it transposes (which can be done by NewArray.', and then gives the mean per column, i.e. your old rows. I chucked everything in the find statement.
As suggested in the comment by Matthias W. it's faster to use the second output of min directly instead of a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
[~,ColNum] = min(abs(mean(NewArray,1)));
MeanColumn = RandomArray(:,ColNum);

How do I find frequency of values appearing in all rows of a matrix in MATLAB?

I have a 161*32 matrix (labelled "indpic") in MATLAB and I'm trying to find the frequency of a given number appearing in a row. So I think that I need to analyse each row separately for each value, but I'm incredibly unsure about how to go about this (I'm only new to MATLAB). This also means I'm incredibly useless with loops and whatnot as well.
Any help would be greatly appreciated!
If you want to count the number of times a specific number appears in each row, you can do this:
sum(indpic == val, 2)
where indpic is your matrix (e.g image) and val is the desired value to be counted.
Explanation: checking equality of each element with the value produces a boolean matrix with "1"s at the locations of the counted value. Summing each row (i.e summing along the 2nd dimension results in the desired column vector, where each element being equal to the number of times val is repeated in the corresponding row).
If you want to count how many times each value is repeated in your image, this is called a histogram, and you can use the histc command to achieve that. For example:
histc(indpic, 1:256)
counts how many times each value from 1 to 256 appears in image indpic.
Like this,
sum(indpic(rownum,:) == 7)
obviously change 7 to whatever.
You can just write
length(find(indpic(row_num,:)==some_value))
and it will give you the number of elements equal to "some_value" in the "row_num"th row in matrix "indpic"

How do i set a row within a matrix to 0?

I have a matlab problem where I need to find the maximum number in a matrix, and then find the next greatest value in the matrix that is not in the same row or column as the previous one.
My thought process is that I will find the maximum value in the matrix and then figure out which row and column it is in and then set the rest of the values in the row and column to 0. so far I have this.
a=rand(5)
[row,column]=find(a==max(max(a)))
I can find which row and column the maximum is but that is about it. Can somebody help me with the next step or a better way to go about writing this program? Thank you!
What you need is:
a(row,:)=0;
So, in total:
a=rand(5)
[row,column]=find(a==max(max(a)))
a(row,:)=0;
[row2,column2]=find(a==max(max(a)))
if you have negatives values in a, you can also do:
a(row,:)=-inf;