For example i have matrix 3X3 [-10 10 20 ; -20 20 10 ; -10 20 20 ] First want to add row1 elements,then row2, then row3,after getting row1,row2,row3 values,then want to add all the values of row1+row2+row3 to get a single value.Plese any help
MATLAB: How i can add all elements inside a matrix
matrices
Related Solutions
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;format compact;fontSize = 20;%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'cameraman.tif';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);if numberOfColorBands > 1 % It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end% Display the original gray scale image.
subplot(1, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off') % Initialize an output image.
noisyImage = grayImage; % Initialize
% Get 5,000 random locations
noiseIndexes = randperm(numel(grayImage), 5000);% Add noise
noisyImage(noiseIndexes) = 255;% Display the noisy image.
subplot(1, 2, 2);imshow(noisyImage, []);title('Noisy Image', 'FontSize', fontSize);axis on;
data = [1901 0 11901 -10 21901 -20 31902 0 -11902 -10 3.51902 -20 1.51903 0 51903 -10 -31903 -20 0 ] ;x = data(:,1) ; y = data(:,2) ; z = data(:,3) ; nx = length(unique(x)) ; ny = length(unique(y)) ; X = reshape(x,[ny,nx]) ; Y = reshape(y,[ny,nx]) ; Z = reshape(z,[ny,nx]) ; pcolor(X,Y,Z) shading interp colorbar
Best Answer