Fill empty matrix with determined values
Show older comments
I am trying to enter the first local maxima of 'matrix1' into 'empty_matrix' over a certain range. There are three possible outcomes of local maxima, nothing (0), a 1X1 matrix and a 2X1 matrix. The issue is 'empty_matrix' wont fill up as desired
matrix1 = rand(50,50,500);
matrix2 = rand(50,1);
empty_matrix = zeros(50,50);
for nx=1:50
for ny=1:50
desired_range = squeeze(matrix1(nx,ny,1:50));
[pks, loc] = findpeaks(desired_range(:));
if pks == 0
empty_matrix(ny,nx) = NaN;
elseif pks == size(zeros(2,1))
empty_matrix(ny,nx) = matrix2(loc(1)); %want the first local maximum
elseif pks == size([])
empty_matrix(ny,nx) = matrix2(loc);
end
end
end
1 Comment
James Knowles
on 15 Dec 2017
Accepted Answer
More Answers (1)
Jos (10584)
on 15 Dec 2017
To check is a variable is empty use the function isempty.
a = []
isempty(a)
You might also want to check you if-elseif-else-end loop. The else is missing :) Are you sure about that?
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!