I have this matrix
v=[4,-2,1;-2,1,-1;-2,3,6]
How can i return the max value of specific col with its row index ? Knowing that i used this function :
[amax,rowIdx]=max(abs(v(k:n,k)),[],1)
but it doesn't work well
here is my code :
v=[4,-2,1;-2,1,-1;-2,3,6]
n=3;
for k=1:n-1
[amax,rowIdx]=max(abs(v(k:n,k)),[],1)
end
If I understand your question correctly, you want to get maximum of third column?
[max_val, max_idx] = max(v(:, 3));
you select the third column from the matrix -> that gives you a single vector. max then operates on this vector and returns the max value together with its position, which is the row index in the original matrix.
Related
The problem is as follow:
I have a k-nearest index matrix. The column of the matrix denotes the number of an image (1:n). The row of index matrix denotes the index of k-nearest pixels correspoinding to the query pixel.
So I am using for-loops to find out whether a-th index is in column b: for example, 3 is in the first column (1-th pixel), then find out whether 1 is in 3-th column, if no, assign value, if yes, assign another value.
The code is here:
for pix = 1:row*col
for i = 1:k
position = find(Index(:, Index(i,pix)) == pix);
if isempty(position) difference = - weight(k, Index(i,pix)) ;
else difference = weight(i,pix) - weight(position, Index(i,pix)) ; end
end
end
Since row*col is very large. I hope to reduce the code into one for-loop. Is there any other method to speed up? Thanks in advance!
I get an image, let say
img=imread('test.jpg')
how can I count all pixel values in each row for a projection on a 1D signal to the right and the pixel values of each column for a projection to the bottom.
does it mean I need to find sum or the pixel in row and column.
what is the statement to sum row pixel and column pixel?
is this statement for sum of row pixel : sum_all = sum(img(:)); if so, what is the statement for sum of column pixel?
you should check the documentation for the sum function in matlab. The second argument specifies the dimension to sum. if dimension=1, then you're summing the rows. If dimension=2, then you want to sum the columns. Therefore, you have the simple code:
% Sum the rows
sum_rows = sum(img);
% Alternate form to sum the rows
sum_rows = sum(img, 1);
% sum the columns
sum_cols = sum(img, 2);
You can slice an n-dimensional array using the img(:,x) notation. the : indicates that you want all of the indexes from that pixel. You could also do img(a:b,x) if you want a subset of one row.
Using this method, you can sum one row of an image as sum(img(:,n)) where n is the row you want to sum. Likewise, for columns, it would be sum(img(n,:)).
I have done the following:
img=imread('test4.jpg');
sum_row = sum(img,2);
this gives me the sum for every row,,,
sum_col = sum(img,1);
this gives me the sum for every column,,,
is that correct?
Yes I got it correct,
so let say i have 2 values here at the end,
what does it mean by If there are periodic highs or lows?
I have created a function that should be able to read in any mxn matrix and gives me the maximum value of the entire matrix (not just per column) and what its indices are.
function [ BIGGEST ] = singlemax( x )
[Largest_values row]=max(x)
[biggest_number column] = max(max(x))
end
This function gives me all the information I need, however it is not very clean as it gets messy the larger the matrix.
The real problem area is printing out the row in which the maxima is located.
Largest_values =
0.7750 0.9122 0.7672 0.9500 0.6871
row =
3 2 3 2 2
biggest_number =
0.9500
column =
4
This is my print out given a random matrix as an input.With the function I have created I cannot read the indices of my max value in any given array using a created function. If I could somehow relate the maximas from each column and there corresponding row (such as making the results a matrix with the column max on top and the row index on bottom, all within the same respective columns )I could display the row of the absolute maximum.
Here's one approach:
value = max(x(:));
[rowIndex,columnIndex] = ind2sub(size(x),find(x==value));
Read the ind2sub documentation for more details.
Edited to modify so that it finds indices of all occurrences of the maximum value.
I have a matrix A with m rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a (with m values). That is, I want A_{i,a_i} = 1. Is there a quick way to do this in Matlab (without a for-loop)?
I solved it using the sub2ind function:
A(sub2ind(size(A), 1:numel(a), a')) = 1
I got a 5 * 5 MATRICE.
I want to find the minimum of (i,j) which corresponds to minimum distance from a fixed point of my grid.
I used:
MATRICE(find(MATRICE(1,:))== min(MATRICE(1,:)),:)
and:
[minVal, minInd] = min(MATRICE(:,1));
MATRICE(minInd,1);
[~,MATRICE_2] = min(MATRICE(:,:));
disp(MATRICE_2) gets me the correct row(i) of the MATRICE but column(j) isn't displayed at all.
I am not sure if I understand correctly, but if you want to find the (row,col) indices of the smallest entry in your array, you can use this
[i,j] = ind2sub(size(MATRICE), find(MATRICE==min(MATRICE(:))))
where A is your matrix. This works by changing your matrix to a vector of 25 elements, looking for the position of the smallest value, and converting that position to a (row,col) pair by using ind2sub.
What you wrote finds minimum entry in every column of your matrix. So, every i entry in MATRICE_2 shows the row index of minimum value in the column i. In other words, column and row indices are
cols = 1:numel(MATRICE_2);
rows = MATRICE_2;
OK i figure it out:
[MinValue, MinIndex] = min(MATRICE(:));
MinSub =ind2sub(size(MATRICE), MinIndex);
MinValue is the minimum value of the MATRICE and MinIndex the index of it plus is pointing the fixed point directly.