Fill empty matrix with determined values

9 次查看(过去 30 天)
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 个评论
James Knowles
James Knowles 2017-12-15
Sorry I realize the example I've provided has multiple matrix size possibilities. The case I am looking for there are only the three matrix sizes listed possible.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2017-12-15
There are some very odd constructs in the code you've written:
desired_range = squeeze(...)
... = desired_range(:)
The squeeze reshapes the original matrix, the (:) reshapes it again, so the squeeze is pointless.
Two issues with:
if x == size(zeros(2,1)) %and if x = size([])
  • first one is you know what size(zeros(2, 1)) is, it's [2 1]. Same with (size([]), it's [0 0], so you may as well write it directly rather than constructing a fixed size matrix then getting its size.
  • second, x == somevector will return a logical vector the same size as somevector. So, the outcome of your test can be one of [true, true], [true, false], [false, true] or [false, false]. Most people do not know what the result of if logical_vector is, so it's almost always an error when they write that. In your case, it will happen to do the right thing, but if you wanted to test for equality of two matrices, you'd use isequal:
if isequal(pks, [2 1])
Except that's not what you want to test at all, you want to see if the number of peaks is equal to a value, in which case:
if numel(peaks) == 2
But since you're doing the same thing regardless if there is one or two peaks (take the first one), there's no point in differentiating between them. So overall, this is how I would have written your code (assuming I understood what you're trying to do:
for nx = 1:size(matrix1, 1) %don't hardcode sizes. Query them
for ny = 1:size(matrix, 2) %note that traditional y is vertical and thus the 1st dimension
desired_range = matrix1(nx, ny, 1:50);
[~, loc] = findpeaks(desired_range(:));
if isempty(loc)
empty_matrix(ny,nx) = NaN;
else
empty_matrix(ny,nx) = matrix2(loc(1)); %want the first local maximum
end
end
end
  1 个评论
James Knowles
James Knowles 2017-12-17
Thank you for providing such clarity on this issue, all your points have been really helpful.

请先登录,再进行评论。

更多回答(1 个)

Jos (10584)
Jos (10584) 2017-12-15
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?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by