Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Help with verifying a while loop

1 Ansicht (letzte 30 Tage)
Amine Ben Ayara
Amine Ben Ayara am 29 Feb. 2016
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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;

Antworten (1)

Walter Roberson
Walter Roberson am 29 Feb. 2016
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 Kommentare
Amine Ben Ayara
Amine Ben Ayara am 29 Feb. 2016
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 am 29 Feb. 2016
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())

Diese Frage ist geschlossen.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by