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:
scale a multidimensional array from within a loop?

Subject: scale a multidimensional array from within a loop?

From: Kirk

Date: 23 Nov, 2009 21:47:19

Message: 1 of 1

I need to "ramp" a covariance matrix within a "parfor" loop for use with mvnrnd function. Rather than a one step jump that results from a simple single multiplier, all members of the matrix receive an new increasing multiplier within each step of the loop. I have written the following to do the scaling from within the loop.

=============================================
function [withScenario]=sRamp(woScenario)
% Will scale variability each year in order to reach an set maximum over
% the number of years defined in the ramp. Creates new covariance matrix
% (withScenario) from the input covariance (woScenario). sRamp askes for the
% increse in variavility (s) over the length of the ramp, and the length of the ramp
% as input

%sInc = input('Enter a maximum multiplier for variability increase for end of ramp');
%rampYrs = input('Enter number of years to ramp variability: ');

sInc = 20;
rampYrs = 200;

% Fill vector with zeros
scenario=zeros(12*rampYrs,1);
% Set first 12 months of year one equal to first 12 months of spinup
% climate fle
firstYr=woScenario(1:12);
% Loop through each year (12 months per year) adding the temp increase to each year
for yr=1:rampYrs
    scenario(((yr-1)*12)+1:12+((yr-1)*12))=firstYr(1:12)+(yr*(sInc/rampYrs));
end
woScenario(end-((12*rampYrs)-1):end) = scenario;
withScenario = woScenario;
==================================================

However, I have only ever done this kind of thing on one dimensional matrices, trying to modify this script to work on a multidimensional matrix is proving challenging. I am getting the error:

"??? Subscript indices must either be real positive integers or logicals." and I thing it is because of the structure of the matix.

I can't seem to wrap my head around how to work on all elements of the following covariance matrix:

val(:,:,1) =

    1.0000 0.9362 0.9162 -0.1859
    0.9362 1.0000 0.7189 -0.1899
    0.9162 0.7189 1.0000 -0.1995
   -0.1859 -0.1899 -0.1995 1.0000


val(:,:,2) =

    1.0000 0.9391 0.8761 -0.2509
    0.9391 1.0000 0.6589 -0.1961
    0.8761 0.6589 1.0000 -0.3211
   -0.2509 -0.1961 -0.3211 1.0000


val(:,:,3) =

    1.0000 0.8912 0.8947 -0.0219
    0.8912 1.0000 0.5976 0.0874
    0.8947 0.5976 1.0000 -0.1785
   -0.0219 0.0874 -0.1785 1.0000


val(:,:,4) =

    1.0000 0.7632 0.9595 -0.0834
    0.7632 1.0000 0.5560 0.2100
    0.9595 0.5560 1.0000 -0.2519
   -0.0834 0.2100 -0.2519 1.0000


val(:,:,5) =

    1.0000 0.7166 0.8933 -0.1296
    0.7166 1.0000 0.3394 0.2646
    0.8933 0.3394 1.0000 -0.4405
   -0.1296 0.2646 -0.4405 1.0000


val(:,:,6) =

    1.0000 0.6967 0.8711 -0.1800
    0.6967 1.0000 0.2899 0.0415
    0.8711 0.2899 1.0000 -0.4640
   -0.1800 0.0415 -0.4640 1.0000


val(:,:,7) =

    1.0000 0.6639 0.8505 -0.1792
    0.6639 1.0000 0.2032 0.0984
    0.8505 0.2032 1.0000 -0.4872
   -0.1792 0.0984 -0.4872 1.0000


val(:,:,8) =

    1.0000 0.6384 0.8557 -0.2010
    0.6384 1.0000 0.1802 0.1017
    0.8557 0.1802 1.0000 -0.5024
   -0.2010 0.1017 -0.5024 1.0000


val(:,:,9) =

    1.0000 0.6822 0.8634 -0.1212
    0.6822 1.0000 0.2416 0.1091
    0.8634 0.2416 1.0000 -0.3877
   -0.1212 0.1091 -0.3877 1.0000


val(:,:,10) =

    1.0000 0.6457 0.8935 -0.0595
    0.6457 1.0000 0.2446 0.3082
    0.8935 0.2446 1.0000 -0.3379
   -0.0595 0.3082 -0.3379 1.0000


val(:,:,11) =

    1.0000 0.8577 0.8958 -0.1026
    0.8577 1.0000 0.5456 0.0685
    0.8958 0.5456 1.0000 -0.2735
   -0.1026 0.0685 -0.2735 1.0000


val(:,:,12) =

    1.0000 0.9061 0.6831 -0.2267
    0.9061 1.0000 0.3790 -0.1647
    0.6831 0.3790 1.0000 -0.2576
   -0.2267 -0.1647 -0.2576 1.0000

Any suggestions on this front would be most appreciated.

Tags for 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