Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix dimensions must agree

Subject: Matrix dimensions must agree

From: Alex Larcombe

Date: 18 Mar, 2010 02:27:05

Message: 1 of 10

Hi,
I am very new to Matlab, and am attempting to debug some code written by a colleague. I keep receiving the following error:

??? Error using ==> minus
Matrix dimensions must agree.

Which stems from this code:

MM_10 = (0.10*(MM_max-MM_min)) + MM_min;
    [time_MM_10, k_temp_10] = max(time(MM<=MM_10));

Is anyone able to help me out, with the information provided, or is further detail required?
Thanks,
Alex.

Subject: Matrix dimensions must agree

From: Wayne King

Date: 18 Mar, 2010 02:56:08

Message: 2 of 10

"Alex Larcombe" <alexanderl@ichr.uwa.edu.au> wrote in message <hns31p$h27$1@fred.mathworks.com>...
> Hi,
> I am very new to Matlab, and am attempting to debug some code written by a colleague. I keep receiving the following error:
>
> ??? Error using ==> minus
> Matrix dimensions must agree.
>
> Which stems from this code:
>
> MM_10 = (0.10*(MM_max-MM_min)) + MM_min;
> [time_MM_10, k_temp_10] = max(time(MM<=MM_10));
>
> Is anyone able to help me out, with the information provided, or is further detail required?
> Thanks,
> Alex.

Hi Alex, the sizes of your matrices (vectors) MM_max and MM_min are not compatible. Put a line

whos MM_max MM_min

right above
MM_10 = (0.10*(MM_max-MM_min)) + MM_min;

and you will see what the sizes of MM_max and MM_min are. It may be a simple matter of transposing one of them.

Wayne

Subject: Matrix dimensions must agree

From: Mohsin Tiwana

Date: 18 Mar, 2010 02:57:09

Message: 3 of 10

Hi!

Need to know your matrix dimensions. But most of the times if you add (.) operator (element to element operation) it will fix the problem. My answer is too general and may not apply to your case. If you tell the matrix dimensions, I will be able to help you more specifically.

Mohsin Tiwana


"Alex Larcombe" <alexanderl@ichr.uwa.edu.au> wrote in message <hns31p$h27$1@fred.mathworks.com>...
> Hi,
> I am very new to Matlab, and am attempting to debug some code written by a colleague. I keep receiving the following error:
>
> ??? Error using ==> minus
> Matrix dimensions must agree.
>
> Which stems from this code:
>
> MM_10 = (0.10*(MM_max-MM_min)) + MM_min;
> [time_MM_10, k_temp_10] = max(time(MM<=MM_10));
>
> Is anyone able to help me out, with the information provided, or is further detail required?
> Thanks,
> Alex.

Subject: Matrix dimensions must agree

From: Mohsin Tiwana

Date: 18 Mar, 2010 02:59:08

Message: 4 of 10

Hi!

Need to know your matrix dimensions. But most of the times if you add (.) operator (element to element operation) it will fix the problem. My answer is too general and may not apply to your case. If you tell the matrix dimensions, I will be able to help you more specifically.

Mohsin Tiwana


"Alex Larcombe" <alexanderl@ichr.uwa.edu.au> wrote in message <hns31p$h27$1@fred.mathworks.com>...
> Hi,
> I am very new to Matlab, and am attempting to debug some code written by a colleague. I keep receiving the following error:
>
> ??? Error using ==> minus
> Matrix dimensions must agree.
>
> Which stems from this code:
>
> MM_10 = (0.10*(MM_max-MM_min)) + MM_min;
> [time_MM_10, k_temp_10] = max(time(MM<=MM_10));
>
> Is anyone able to help me out, with the information provided, or is further detail required?
> Thanks,
> Alex.

Subject: Matrix dimensions must agree

From: Angela

Date: 18 Mar, 2010 03:01:21

Message: 5 of 10

My first guess would be that MM_max has different dimensions to MM_min.
Have you checked that they are the same size? i.e.
> whos MM_max
>whos MM_min

Subject: Matrix dimensions must agree

From: Alex Larcombe

Date: 18 Mar, 2010 03:07:08

Message: 6 of 10

Thanks for the help everyone. I have tried whos MM_max and whos MM-min:

Name Size Bytes Class Attributes

  MM_max 1x1 8 double
  MM_min 1x1 8 double

I assume that this means that MM_max and MM_min have the same dimensions?

"Angela " <ang.c.white@removethisgmail.com> wrote in message <hns521$h94$1@fred.mathworks.com>...
> My first guess would be that MM_max has different dimensions to MM_min.
> Have you checked that they are the same size? i.e.
> > whos MM_max
> >whos MM_min

Subject: Matrix dimensions must agree

From: Angela

Date: 18 Mar, 2010 04:42:03

Message: 7 of 10

"Alex Larcombe" <alexanderl@ichr.uwa.edu.au> wrote in message <hns5cr$mh0$1@fred.mathworks.com>...
> Thanks for the help everyone. I have tried whos MM_max and whos MM-min:
>
> Name Size Bytes Class Attributes
>
> MM_max 1x1 8 double
> MM_min 1x1 8 double
>
> I assume that this means that MM_max and MM_min have the same dimensions?

Yes.

We might be able to help more if you shared more of your script, if possible. Maybe the error is further up?

Subject: Matrix dimensions must agree

From: Alex Larcombe

Date: 18 Mar, 2010 05:16:05

Message: 8 of 10

Hi,

Here is the script from the start until the error lines:
Any assistance is much appreciated.

function calc_delayfn()

warning off;

FIG_TMPCALC = 1;
FIG_DELAYFN = 2;

[delayfiles, delaypath] = uigetfile('*.txt','Select delay text files','MultiSelect','on');

if iscell(delayfiles) == 1
    numfiles = length(delayfiles);
else
    numfiles = 1;
end

for k = 1:numfiles
    if iscell(delayfiles) == 1
    file_curr = strcat(delaypath, delayfiles(k));
    fprintf(1,'%s\n',file_curr{1});
    [time,volume,O2,CO2,flow,MM] = textread(file_curr{1},'%d%f%f%f%f%f','delimiter','\t','headerlines',1);
    else
    file_curr = strcat(delaypath, delayfiles);
    fprintf(1,'%s\n',file_curr);
    [time,volume,O2,CO2,flow,MM] = textread(file_curr,'%d%f%f%f%f%f','delimiter','\t','headerlines',1);
    end
    
       figure(FIG_TMPCALC);
    clf(FIG_TMPCALC);
    
    subplot(3,1,1), plot(time, flow);
    title(file_curr);
    ylabel('Flow (L s^{-1})');
    xlim([0 time(end)]);
    ylim([-0.2 0.2]);

        fprintf(1,'Click on start for flow calculation...\n');
    [time_start,flow_start] = ginput(1);
    fprintf(1,'Click on end for flow calculation...\n');
    [time_end,flow_end] = ginput(1);
    fprintf(1,'Start flow is %1.4f L/s\n',flow_start);
    fprintf(1,'End flow is %1.4f L/s\n',flow_end);
    flow_mean(k,1) = mean(flow((time>time_start)&(time<time_end)));
    fprintf(1,'Mean flow between %3.2f and %3.2f s is %1.4f L/s.\n\n',time_start/1000,time_end/1000,flow_mean(k));
   
    subplot(3,1,1), hold on;
    plot(time_start, [-0.2:0.001:0.2], 'r-');
    plot(time_end, [-0.2:0.001:0.2], 'r-');

    % Determine start of MM and CO2, respectively
    subplot(3,1,2), hold off;
    plot(time,MM), ylabel('MM (g mol^{-1})'), xlim([0 time(end)]), ylim([26 32]);
    subplot(3,1,3), hold off;
    plot(time,CO2), ylabel('CO_2 (%)'), xlim([0 time(end)]), ylim([-2 6]);
    xlabel('Time (ms)');

    % For the MM signal, due to signal fluctuation at
    % baseline, there is difficulty in obtaining a true start.
    % Thus, treat the first and last 1000 ms as baselines
    MM_min = mean(MM(time<=1000));
    MM_max = mean(MM(time>=(time(end)-1000)));
   
    % Using 10% of signal excursion
    % Consider all points below or equal to 10% of MM signal excursion
    % and take the latest point
    % whos MM_max MM_min
    MM_10 = (0.10*(MM_max-MM_min)) + MM_min;
    [time_MM_10, k_temp_10] = max(time(MM<=MM_10));

Subject: Matrix dimensions must agree

From: Alex Larcombe

Date: 18 Mar, 2010 05:25:06

Message: 9 of 10

Hi,

Here is the script from the start until the error lines:
Any assistance is much appreciated.

function calc_delayfn()

warning off;

FIG_TMPCALC = 1;
FIG_DELAYFN = 2;

[delayfiles, delaypath] = uigetfile('*.txt','Select delay text files','MultiSelect','on');

if iscell(delayfiles) == 1
    numfiles = length(delayfiles);
else
    numfiles = 1;
end

for k = 1:numfiles
    if iscell(delayfiles) == 1
    file_curr = strcat(delaypath, delayfiles(k));
    fprintf(1,'%s\n',file_curr{1});
    [time,volume,O2,CO2,flow,MM] = textread(file_curr{1},'%d%f%f%f%f%f','delimiter','\t','headerlines',1);
    else
    file_curr = strcat(delaypath, delayfiles);
    fprintf(1,'%s\n',file_curr);
    [time,volume,O2,CO2,flow,MM] = textread(file_curr,'%d%f%f%f%f%f','delimiter','\t','headerlines',1);
    end
    
       figure(FIG_TMPCALC);
    clf(FIG_TMPCALC);
    
    subplot(3,1,1), plot(time, flow);
    title(file_curr);
    ylabel('Flow (L s^{-1})');
    xlim([0 time(end)]);
    ylim([-0.2 0.2]);

        fprintf(1,'Click on start for flow calculation...\n');
    [time_start,flow_start] = ginput(1);
    fprintf(1,'Click on end for flow calculation...\n');
    [time_end,flow_end] = ginput(1);
    fprintf(1,'Start flow is %1.4f L/s\n',flow_start);
    fprintf(1,'End flow is %1.4f L/s\n',flow_end);
    flow_mean(k,1) = mean(flow((time>time_start)&(time<time_end)));
    fprintf(1,'Mean flow between %3.2f and %3.2f s is %1.4f L/s.\n\n',time_start/1000,time_end/1000,flow_mean(k));
   
    subplot(3,1,1), hold on;
    plot(time_start, [-0.2:0.001:0.2], 'r-');
    plot(time_end, [-0.2:0.001:0.2], 'r-');

    % Determine start of MM and CO2, respectively
    subplot(3,1,2), hold off;
    plot(time,MM), ylabel('MM (g mol^{-1})'), xlim([0 time(end)]), ylim([26 32]);
    subplot(3,1,3), hold off;
    plot(time,CO2), ylabel('CO_2 (%)'), xlim([0 time(end)]), ylim([-2 6]);
    xlabel('Time (ms)');

    % For the MM signal, due to signal fluctuation at
    % baseline, there is difficulty in obtaining a true start.
    % Thus, treat the first and last 1000 ms as baselines
    MM_min = mean(MM(time<=1000));
    MM_max = mean(MM(time>=(time(end)-1000)));
   
    % Using 10% of signal excursion
    % Consider all points below or equal to 10% of MM signal excursion
    % and take the latest point
    % whos MM_max MM_min
    MM_10 = (0.10*(MM_max-MM_min)) + MM_min;
    [time_MM_10, k_temp_10] = max(time(MM<=MM_10));

Subject: Matrix dimensions must agree

From: Bruno Luong

Date: 18 Mar, 2010 07:22:05

Message: 10 of 10

Type
dbstop if error

at the command line and rerun your code. The debugger will break when the error occurs, then you can check the size of your variable to see what's wrong (My guess is one of the variable is empty).

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us