problem with storing in an array

조회 수: 15 (최근 30일)
第四
第四 2024년 7월 16일
댓글: 第四 2024년 7월 16일
% 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.

채택된 답변

Steven Lord
Steven Lord 2024년 7월 16일
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.
  댓글 수: 1
第四
第四 2024년 7월 16일
非常感谢您的回复,一目了然

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2024년 7월 16일
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 2024년 7월 16일
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
  댓글 수: 1
第四
第四 2024년 7월 16일
原来如此,非常感谢

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by