issue with matlab function

1 view (last 30 days)
Hossam Mosbah
Hossam Mosbah on 26 Jun 2022
Edited: dpb on 27 Jun 2022
Matlab function did not store the previous vectors, it is only store the current vector and replace it other vectors by zeros? Thanks
I am trying to save vector of thresholdd in each function iteration
however, matlab function set the vector for current iteration and replace the other previous vector with zeros. The equations are really comlicated
i can put them all here,
function iteration, each time, we store vectors of the belwo variables
npb=from 1 to 30 days
PPTH(:,npb)=thresholdd
PBF(:,npb)=Pbefore;
PAF(:,npb)= Pafter;
PPTH1=reshape(PPTH, [],1)
PBF1=reshape(PBF, [],1)
PAF1=reshape(PAF, [],1)
zero vectors for previous iteration is not acceptable
PPTH=
0 0 0 208.3177
0 0 0 208.3177
0 0 0 208.3177
0 0 0 208.3177
0 0 0 208.3177
0 0 0 208.3177
0 0 0 208.3177
  2 Comments
Hossam Mosbah
Hossam Mosbah on 26 Jun 2022
I added a few equations and trying to explain.

Sign in to comment.

Answers (1)

dpb
dpb on 26 Jun 2022
Edited: dpb on 27 Jun 2022
It's not MATLAB's fault, it did exactly what you told it to do...just your instructions weren't what you intended! :)
While we don't have sufficient data to run any of your code, a basic problem in the code snippet shown
npb=from 1 to 30 days
PPTH(:,npb)=thresholdd
...
besides the fact that npb=from 1 to 30 days is not valid MATLAB syntax is that
PPTH(:,npb)=thresholdd
will put each new value into the COLUMN npb which will, presuming the above is intended to represent a for...end construct over npb into each column successively. W/O knowing what thresholdd is, there's no way to know what the end result of the assignment actually is.
If it is a vector, then PPTH will end up as a 2D array; if it is a scaler, it will build a row vector which then is RESHAPEd to another row vector, a completely useless operation.
One should write something more like
N=30; % use variables; don't bury magic constants in multiple places in code
PPTH=zeros(1,N); % preallocate a row vector
for npb=1:N
PPTH(npb)=thresholdd; % and put the value into the array
...
The above is generic code; from the snippet provided there's nothing to indicate that thresholdd isn't just a constant; if that is so, there's probably little point in making a whole array of them, but in MATLAB that operation, if needed is simply to assign the constant to the array...
PPTH=repmat(thresholdd,1,npb); % create row vector of npb elements containing value thresholdd
or
PPTH=thresholdd*ones(1,npb); % alternate syntax for same thing -- any number of ways are possible
As for the complaint about zeros and the numbers shown; we simply have insufficient actual code to be able to know what was done and how to go about fixing it -- but a most common beginner's mistake is to simply not index into the array; your indexing issues above strongly indicate there's where your issues are, but we'll have to see actual code that was run to know just how to fix syntax; can't even guess from what have so far; it wouldn't produce such a result on its own.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!