How to record the intermediate result of a loop for in matrix, with each iteration?

Asked

Viewed 3,663 times

0

below follows the attempt of a code to calculate the following situation:

  • I have two vectors: "q_qp" and "pe", I need each element of the vector "pe" to multiply all elements of "q_qp", for example: the value 9.528007810207796E-5 of "pe" multiplies all values of "q_qp" and returns me a resulting vector in the same dimension of "q_qp" (33 x 1)and thus successively up to the last value of "pe".

Since the dimension of "pe" is 22 x 1 and "q_qp" is 33 x 1, I will have 22 33 row column vectors at the end of the loop.

The question is, how can I make so that each iteration the result is written in an orderly manner in a matrix of zeroes with dimension of 33 x 22, i.e., first iteration records the vector in the first column, second iteration records the vector in the second column, and so on. I’m using Scilab and I was able to generate the vectors with the loop, but I can not record the result in the matrix. If anyone can help me, I’d be most grateful.

clc
clear

q_qp= [0 0.03 0.1 0.19 0.31 0.47 0.66 0.82 0.93 0.99 1 0.99 0.93 0.86 0.78 0.68 0.56 0.46 0.39 0.33 0.28 0.207 0.147 0.107 0.077 0.055 0.04 0.029 0.021 0.015 0.011 0.005 0]' 

pe= [9.528007810207796E-5
0.015612153888687455
0.04127114106369897
0.06865675895676779
0.10234219137829395
0.149156938574736
0.2221660433295402
0.3516835634365342
0.6253621948431046
0.9021458528207191
2.49812553600102
1.4110166279515943
0.47462270737544116
0.2936798150556328
0.2010333119231717
0.1471011647128549
0.1128517315033946
0.08968709798222249
0.07325270551003342
0.06114745114515898
0.0]

[l c]= size(pe)
Q1= zeros(33, l)
[la ca]= size(Q1)

for i= 1:l
    for j= 1:c

        a= pe(i ,1)
        disp(a)
        Q= a .* q_qp
        disp(Q)
        B(i)= [Q(i)] ***>>> Aqui eu gostaria que B recebesse o cálculo da primeira iteração na primeira coluna da matriz, na segunda iteração o vetor fosse gravado na segunda coluna da matriz B, e assim sucessivamente até completar o for.*** 
    end

    for m= 1:la  ***>>> esse segundo for foi uma tentativa de gravar os vetores na matriz, mas não deu certo. ele grava somente o resultado da última iteração, ou seja a multiplicação entre o último valor de "pe" e o vetor "q_qp".*** 
        for n= 1:ca
            Q2(m, n)= [Q(i)] 
        end
    end
end
  • It is impossible to read your question, you could format it better. It has all the options on top of the editor.

  • Man, the logic for this is simple, I don’t understand that language. If qtd1 and qtd2 are the terms quantity of matrix 1 and matrix 2: for (int n=0; n<qtd1; n++) { for (int k=0; k<qtd2; k++) { matriz_r[$n][$k] = matriz1[$n]*matriz2[$k]; } }

  • Thanks for the comment friend, however, the language of Scilab does not accept this logic.

1 answer

1

In Matlab the following syntax is used (I imagine that in Scilab should not be very different):

outputMatrix = zeros(33, 22);
for i = 1:22
    scaledVector = pe(i)*q_qp;
    outputMatrix(:, i) = scaledVector';
end        

Note that when using a scaledVector vector as an intermediate step to transpose the result. If this were not done, it would generate a size compatibility error. But this step can be suppressed if the language directly accepts the command outputMatrix(:, i) = pe(i)*q_qp'.

  • Thank you for answering, but when I enter this code I have the following error: Submatriz set incorrectly. at line 35 of exec file called by :

  • I was thinking, if I could get every iteration of "i" the vector generated by the product of "i" by "q_qp" saved in a variable, it would be easier to try a solution. Would it be possible to create a new variable and give it value every iteration ? Thank you.

  • Actually that’s exactly what the Ector scaledVector is, a new variable. I don’t know exactly at what point the error is occurring, but I think you should test things like declaring the Ector scaledVector before the for and test new things in the index like: outputMatrix = zeros(33, 22); scaledVector = zeros(1, 33); for i = 1:22, scaledVector(1, :) = pe(i)*q_qp; outputMatrix(:, i) = scaledVector'; end.

  • Thank you for answering, I will try here, as soon as I have answers posted on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.