필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Help with verifying a while loop

조회 수: 1 (최근 30일)
Amine Ben Ayara
Amine Ben Ayara 2016년 2월 29일
마감: MATLAB Answer Bot 2021년 8월 20일
I have been trying to get a while loop to run all the way through but I keep getting an error; "Error using horzcat Dimensions of matrices being concatenated are not consistent." although I am not even using "horzcat" at all in my loop. Here is what I wrote, and the objective is very simple, compare two values and pick the row correspondent to the highest values of the two being compared and put it into an output matrix M.
MI4(:, 2)=MI4(:,2)+1;
M(1, :)=[MI1(1, :),0];
for i=1:14647;
j=i+1;
max_marg_cum=max(MI4(1:i, 3));
max_ratio=MI1(j, 3);
% if the next pixel in the ratio table is bigger
%the output table will take the whole row for the next pixel in the ratio table
while max_ratio < max_marg_cum;%if the next pixel in the marginal table is bigger
[row_max_marginal,col_max_marginal] = find(MI4 == max_marg_cum);% find the location of the max marginal
max_marginal=MI4(row_max_marginal, :);% extract the max (whole row) in the marginal table
M=[M;max_marginal,1];%the output table will take the whole row for the next pixel in the marginal table
rd_max_plus_1=max_marginal(1, 2)+1 ;% increse the row id of the max marginal row by 1;
[row_max_original,col_max_original] = find(MI5== max_marginal(1,1));%locate the pixel value the same as the max just identified in the marginal table
MI4(row_max_marginal,: )=[MI5(row_max_original, 1), rd_max_plus_1, MI5(row_max_original, rd_max_plus_1)];
max_marg_cum=max(MI4(1:i, 3));
end;
M=[M;MI1(j, :),0];
end;

답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 29일
M=[M;max_marginal,1];
is equivalent to
M = vertcat(M, horzcat(max_marginal,1))
so horzcat is being called. Likewise,
MI4(row_max_marginal,: )=[MI5(row_max_original, 1), rd_max_plus_1, MI5(row_max_original, rd_max_plus_1)];
is equivalent to
MI4(row_max_marginal,: ) = horzcat(MI5(row_max_original, 1), rd_max_plus_1, MI5(row_max_original, rd_max_plus_1));
so horzcat is being called.
In particular, your rd_max_plus_1 is a scalar value, but your MI5(row_max_original, 1) has as many rows as length(row_max_original), and row_max_original is derived from find(MI5== max_marginal(1,1)) and so is going to have a length equal to the number of items found. Your code breaks if there are duplicate copies of the maximum.
  댓글 수: 2
Amine Ben Ayara
Amine Ben Ayara 2016년 2월 29일
oh!!!!!!!!!! man! you are a genius! Ive been looking at this forever without any clues. So do you have any recommendations to where I can avoid the breaking part? I mean another way of avoiding this problem, knowing that max(vector) can only return one maximum value right?
Walter Roberson
Walter Roberson 2016년 2월 29일
max() can only return one value.
Have you considered using the two-output form of max?
[max_marg_cum, maxidx] = max(MI4(1:i, 3));
then even if there are duplicates of the maximum, maxidx will only be one of them (which one depends upon your MATLAB version and upon additional options you can pass to max())

이 질문은 마감되었습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by