Error using .* Matrix Dimensions must agree

3 visualizzazioni (ultimi 30 giorni)
Stephanie Goldstein
Stephanie Goldstein il 25 Ago 2014
Commentato: Jan il 4 Lug 2017
Hello! I am attempting to find the area of grid cells using two vectors, 1)latitude which is 224 X 1 and 2)longitude which is 240 X 1. The code I am using to do so is
R=6371
x=diff(latitude1)
y=diff(longitude)
Area_grid_points=(((2*pi*R)/360)^2).*cos(latitude1).*x.*y
Unfortunately when I use this code I receive the error "Error using .* Matrix Dimensions must agree." I am assuming that I receive this error because cos(latitude1) produces an array of 224*1 which is different than that produced by diff(longitude1) which is 240 X 1. Can anyone direct me in the right direction to correct this discrepancy? Thanks so much!

Risposte (3)

Kelly Kearney
Kelly Kearney il 25 Ago 2014
Well, diff is always going to return a result 1 element shorter than the input, since it's measuring the distance between elements. In your case, you'll have to decide what latitude you want to correspond to each grid cell: top? bottom? middle?
I usually choose the midpoint for things like this:
R=6371
x=diff(latitude1)
y=diff(longitude)
latmid = (latitude(1:end-1)+latitude(2:end))./2;
Area_grid_points=(((2*pi*R)/360)^2).*cos(latmid).*x.*y;
  3 Commenti
Stephanie Goldstein
Stephanie Goldstein il 25 Ago 2014
Hi Kelly. Thanks for your response. Unfortunately, I still received the error "Error using .* Matrix dimensions must agree." Do you know how I can make the matrix dimensions equal to one another?
Kelly Kearney
Kelly Kearney il 25 Ago 2014
Modificato: Kelly Kearney il 25 Ago 2014
I was assuming that lattitude and longitude were the same size, but apparently not. Assuming that both latitude and logitude are row vectors, representing the edges of a rectangular (in lat/lon space) region, perhaps you want:
R = 6371
x = diff(latitude1);
y = diff(longitude);
latmid = (latitude(1:end-1)+latitude(2:end))./2;
Area_grid_points = (2*pi*R/360)^2 .* y' * (cos(latmid).* x);
Note that my use of matrix multiplication with a column and row vector is a shortcut for expanding the two vectors into a full grid.

Accedi per commentare.


José-Luis
José-Luis il 25 Ago 2014
Modificato: José-Luis il 25 Ago 2014
If you want element by element multiplication you should use .* instead of *. whether you want this or matrix multiplication, the number of elements and size of your array should be congruent.
Maybe you are looking for something like bsxfun() instead:
bsxfun(@times, latitude, longitude')
  2 Commenti
Stephanie Goldstein
Stephanie Goldstein il 25 Ago 2014
Hi Jose-Luis. I tried using .* and received an error of "Error: Unexpected MATLAB operator." Here is what I really need help on: how can I make the number of elements and size of my arrays congruent? Also, can you please explain to me what bsxfun(@times, latitude, longitude') does? Thanks so much.
José-Luis
José-Luis il 25 Ago 2014
Well, I don't know. How do you get the latitude and longitude vectors in the first place.
doc bsxfun

Accedi per commentare.


Sajjad Ali
Sajjad Ali il 4 Lug 2017
Modificato: Sajjad Ali il 4 Lug 2017
I am also getting error Error using . Matrix dimensions must agree.* in following line of code _[m,n]=size(thick_image(:,:,1));
indTerm=sub2ind([m,n],CentroidTerm(:,1),CentroidTerm(:,2));
Z=zeros(m,n);
Z(indTerm)=1;
ZTerm=Z.*ROI';
[CentroidTermX,CentroidTermY]=find(ZTerm);_ can any one help me to resolve this issue?
  2 Commenti
Walter Roberson
Walter Roberson il 4 Lug 2017
You do not say what size of ROI is
Jan
Jan il 4 Lug 2017
Please create a new thread for a new question and remove this message here. Otherwise it gets unclear to which question an answer belongs and the Accepted status is meaningless. Thanks.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by