Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Hi everyone, I need your help on my two questions.

1 view (last 30 days)
Engdaw Chane
Engdaw Chane on 15 Feb 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
Q1,
I am working on 30 years monthly data. I have a 3D matrices A=194 x 201 x 360 rows, columns and months respectively.
I am interested only in months from April to September in every year. I need to extract these months from all years. I tried the following:
for year=1:30;
April=(year-1)*12+4;
Sept=(year-1)*12+9;
k(:,:,year)=A(:,:,April:Sept);
for kj=size(k,3);
d(:,:,kj)=A;
end
end
I am getting “Subscripted assignment dimension mismatch.”
Q2, I have two 3D matrices.
B=194 x 201 x 360; C=194 x 201 x 360;
I need to divide B by C
D(:,:,:)= B(:,:,:)./ C(:,:,:);
I am getting Inf values for all rows and columns.
I tried the following to find zero values and replace by 1 in the denominator not to get Inf values in the output matrix.
C(C==0) = 1;
I am still getting the Inf in all rows and columns.
Thank you.
Kindly, Engdaw

Answers (1)

Bob Thompson
Bob Thompson on 15 Feb 2018
Q1
You're getting dimension mismatch because you're trying to put multiple sheets of A into a single sheet of k.
k(:,:,year)=A(:,:,April:Sept);% where k being called is 194x201x1, but A being called is 194x201x6
Q2 Your check of C==0 will not come up positive unless all values of C are 0. Also, if you have numbers in C and you are still getting all rows and all columns as INF then there is another issue. What type of division are you trying to do? Are you trying to divide the actual arrays by each other, as in matrix math, or are you simply trying to divide the corresponding values.
D(1,1,1) = B(1,1,1)/C(1,1,1)
D(2,1,1) = B(2,1,1)/C(2,1,1)
etc...
  2 Comments
Engdaw Chane
Engdaw Chane on 15 Feb 2018
@Bob Nbob, Thank you.
Q1, I tried the following:
% code
k(:,:,April:Sept)=A(:,:,April:Sept);
and I am getting "Subscript indices must either be real positive integers or logicals." Please suggest me how I can select months in all 30 years.
Q2
What I need to do is the following as you noticed it.
D(1,1,1) = B(1,1,1)/C(1,1,1)
D(2,1,1) = B(2,1,1)/C(2,1,1) etc...
I need to do the following: F=G(1,1,1).^H(1,1,1)
Is the following correct; F=G(:,:,:).^H(:,:,:) ? Suggest me the solutions please.
Kindly, Engdaw
Bob Thompson
Bob Thompson on 15 Feb 2018
Edited: Bob Thompson on 15 Feb 2018
Q1
Double check that April and Sept are coming out as numbers.
To save the data for multiple years, you can either save it to multiple arrays, or you can have a multiplication added to your indexing to consider the year. It's ugly, and you might have to mess with the values a bit to perfect it, but the method should work to just add all the consecutive years as sheets.
k(:,:,April+(year-1)*6:Sept+(year-1)*6) = ...
Q2
Matrix and array functions are admittedly not my strong suit. From what I understand using . with an operation means the individual terms are considered, rather than matrix operations which is conducted without the period. I would suggest checking this though with something smaller, maybe a 2x2x2 rather than your full size arrays. If nothing else, you can use a loop, or two, or three to just perform the individual operations "manually".

Community Treasure Hunt

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

Start Hunting!