calculate the number of pixel in a row and column - matlab

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?

Related

Find rows that contain only zeros

I have a huge matrix in MATLAB. Now some rows contain only zeros.
Can I use the function find to find all the rows which contain only zeros ?
You can use any to find any rows that have non-zeros and then negate the result. We use the second input to specify that we want to apply the operation across the columns (the 2nd dimension).
row_has_all_zeros = ~any(data, 2)
If you want the indices instead of the logical array, just apply find to the result:
indices = find(row_has_all_zeros);
If you need, you can obtain more speed (depending on your dataset) by first looking for partial zero rows (in this example length 10) and computing further with the selection of rows.
row_has_first10_zeros = sum(data(:,1:10),2);
row_has_all_zeros = sum(data(~rows,:),2);
indices = find(~row_has_first10_zeros)
indices = indices(~row_has_all_zeros)
n = length(matrix);
This line will give you the number of rows in a matrix.
ids = setdiff(1:n,find(sum(matrix,2)));
ids will give you the row numbers(indexes) which contain only zeros.

How to plot numeric values in a column when values in another column are equal to specific number

How can I get the numeric values in a column (let's say column 10) when the numeric values in another column (let's say column 9) are equal to a specific number and plot this in a graph.
e.g., When values of column 9 == 4, get the corresponding value of column 10 and plot. I am using row index number as a marker for time.
I am plotting all of column 10 to get a waveform then I want to use the data of column 9 to add markers to my waveform that are representative of a command occurring at a certain point in time.
Here is my code:
E = csvread('Experiment_at_10_45_1.csv');
[signal_rows, signal_columns] = size(E);
t=(1:signal_rows)/128; %128 samples per second
%% SNR plot for down frequency
plot(t,E(:,13),'k')
I hope my explanation is clear, as I have attempted to use a minimum working example of my code for the first time.
You'll want to use logical indexing to do this. You want to first create an array of 0 (false) and 1 (true) values where column 9 is equal to the value you want.
bool = E(:,9) == 4;
Then you'll want to use this 0 and 1 array as the row index. This will grab only the rows where column 9 was equal to 4. This is referred to as logical indexing.
E(bool, 10)
Then you can plot this
plot(t(bool), E(bool, 10))
As pointed out though, it is possible that the values aren't exactly to 4 due to floating point representation. To get around this, you just want to check if they are "close enough" using a very small epsilon.
bool = abs(E(:,9) - 4) < 1e-12;

Matlab_max value in each col

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.

How to read the indices of my max value in any given array using a created function

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.

Set a value of a specific column for each row of a matrix

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