cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

For loop iterating over a matrix

SB_9890617
4-Participant

For loop iterating over a matrix

I'm trying to iterate over a matrix and for some reason I can't seem to generate the new matrix. It just spits out the last value of the iteration.

Looking at other posts I think I have something wrong with the indices of the matrix, but I can't quite figure it out.

 

SB_9890617_0-1617810428796.png

 

ACCEPTED SOLUTION

Accepted Solutions
-MFra-
21-Topaz II
(To:SB_9890617)

8 REPLIES 8
-MFra-
21-Topaz II
(To:SB_9890617)

SB 98 answer.jpg

SB_9890617
4-Participant
(To:-MFra-)

Thank you, very much!

SB_9890617
4-Participant
(To:-MFra-)

(Nvm, solved it)

 

Hello,

Sorry to ask again.

I thought I had it figured it out, but when trying to replicate it on mathcad 15 it refuses to work.

I have one file that works and another that doesn't.

mathcad_mistake.PNG

I've spent too much time trying to figure it out, and I'm getting nowhere.

 

 

LucMeekes
23-Emerald III
(To:SB_9890617)

1.

It is very hard to debug pictures. => You should ALWAYS attach the worksheet.

2.

The  left one (the one that works) normally shouldn't work, because the last statement is an assignment to the last element of w, it should result in that last element's value. However I (seem to) see that you're using two different v's in the program (one to define the range and to index the y, the other as an index or subscript to w).

3.

To the right, the first one does not work for the reason as explained under 2. Now you're using the same v in all three places.

The second isn't right, you should not include the single 'w' inside the for loop, but outside of it.

The third is (or, at least, looks like) how it should be. I wonder why that one does not work.

 

One word of advice: The programming tools has this nice item called 'return', you should use it to define the return value of the function, that way you have better control over what is returned,

LucMeekes_0-1620753607016.png

and you can see when you took the wrong route:

LucMeekes_1-1620753629084.png

Success!
Luc

SB_9890617
4-Participant
(To:LucMeekes)

Sorry I completely forgot to upload the files.

Thank you for the quick reply!

LucMeekes
23-Emerald III
(To:SB_9890617)

The result of a program is the last statement executed. In your case that's the assignment of the last element to the array omega. So the result is that last element.

If you add just omega after the for loop, as what MFra showed, you get the entire array.

Note that there is a specific keyword: 'return' that you can use to define the result of a function. Put anywhere in the function, the item placed as the argument to 'return' is the result of the program. Also be aware that 'return' also stops execution of the program. So if you use it within a program construct (If, For or whatever) the program stops there.

But putting it on the last line of a program certainly is no crime.

 

Success!
Luc

SB_9890617
4-Participant
(To:LucMeekes)

Thank you very much for the in depth explanation!

ttokoro
20-Turquoise
(To:SB_9890617)

image.pngimage.pngimage.pngimage.png

Announcements

Top Tags