problem with storing in an array

1 view (last 30 days)
第四
第四 on 16 Jul 2024
Commented: 第四 on 16 Jul 2024
% The previous code was omitted
% Part of the code for the preliminary detection of extreme points
num = 0;
extreme_point = [];
for oct_i = 1 : octave
dog = dog_pyr{oct_i};
[dog_r, dog_c, dog_page] = size(dog);
for r = 6 : dog_r - 5
for c = 6 : dog_c - 5
for page_i = 2 : dog_page - 1
dog_near = zeros(3, 3, 3);
dog_near(:, :, 1) = dog(r - 1 : r + 1, c - 1 : c + 1, page_i - 1);
dog_near(:, :, 2) = dog(r - 1 : r + 1, c - 1 : c + 1, page_i);
dog_near(:, :, 3) = dog(r - 1 : r + 1, c - 1 : c + 1, page_i + 1);
point_vale = dog_near(2, 2, 2);
if (point_vale == max(dog_near(:))) || (point_vale == min(dog_near(:)))
num = num +1;
sigma_i = k ^ (page_i -1) * sigma0;
extreme_point(num, :) = [oct_i, page_i, r, c, sigma_i, 0]; % ???
end
end
end
end
end
hi, I'd like to ask you a questio, about the last line "extreme_point(num, :) = [oct_i, page_i, r, c, sigma_i, 0];", Pls if it is written "extreme_point(num) = [oct_i, page_i, r, c, sigma_i, 0];", an error message that the assignment cannot be performed because the index on the left is incompatible with the size on the right will appear.
May I ask why this error occurs?
At the beginning of learning not quite understand, this is about the sift algorithm part of the content, the overall problem is all machine rollover, please understand.

Accepted Answer

Steven Lord
Steven Lord on 16 Jul 2024
To take a smaller example:
A = magic(4)
A = 4x4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What does indexing into A using one index do?
A(3)
ans = 9
If you count down the columns, 9 is the third element of A.
What does indexing into A using two indices, one of which is a colon, do?
A(3, :)
ans = 1x4
9 7 6 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This is all the columns in the third row of A.
See this documentation page for more information and examples.
So why did your code error? What would happen if you tried to stuff six regular eggs into one cup of an egg carton? Would they fit (assuming you're not able to take the eggs out of their shells)? No. Same thing here: you can't stuff multiple elements into one element of a matrix.
A(3, :) = [99 -1 42 round(100*pi)] % Can assign 4 elements on the right to 4 elements of A
A = 4x4
16 2 3 13 5 11 10 8 99 -1 42 314 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(1) = [99 -1 42 round(100*pi)] % Cannot assign 4 elements on the right to 1 element in A
Unable to perform assignment because the left and right sides have a different number of elements.

More Answers (2)

dpb
dpb on 16 Jul 2024
In
extreme_point(num, :) = [oct_i, page_i, r, c, sigma_i, 0];
the RH side is a vector of 6 elements; as written, the LH side puts that into a row of a (growing) 2D array.
If the LH side subscripting expression is written as only (num), you would be trying to put six things into a single location -- that doesn't work. The only way to have a single subscript would be if you were to put the vectors each into a single cell in a cell array --
extreme_point{num} = [oct_i, page_i, r, c, sigma_i, 0];
would work, but note the difference between the two expressions. The "curlies" {} around the subscript create a cell array, which can hold an array.

Jatin
Jatin on 16 Jul 2024
Hi,
This error usually occurs due to a mismatch in the dimensions of array you are trying to assign values to.
In this case the left-hand side i.e.,extreme_point(num)” is trying to assign an element to extreme_point at numth index, whereas the right-hand side i.e.,[oct_i, page_i, r, c, sigma_i, 0]” is not an element but a row vector. Hence, we are getting an error stating “the assignment cannot be performed because the index on the left is incompatible with the size on the right.”
The right way to assign is to use “extreme_point(num,:)” which says assign a row vector at the numth row of “extreme_point”.
Kindly refer this documentation for more understanding of multidimensional array in MATLAB

Community Treasure Hunt

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

Start Hunting!