# Updating values with for-loop

1 ビュー (過去 30 日間)
Moe 2014 年 5 月 4 日

Hello everyone,
I want to update the matrix "e" according to the new value of matrix "d" for each iteration.
Matrix "d" is generated in each iteration according to the new maximum value if matrix "f"
clear all
clc
d = zeros(2,4);
a = [2 2;1 1;4 1;1 1;4 2;1 1;3 2;3 1;2 1;1 2];
b= [0 0 0 0;0 1 0 0;1 0 0 0;0 0 0 0;0 0 0 1;0 0 0 0;1 0 0 0;0 0 0 0;0 0 0 0;0 0 0 1;1 0 0 0;0 0 0 0;0 0 0 0;0 0 1 0;0 0 1 0;0 0 0 0;0 1 0 0;0 0 0 0;0 0 0 0;1 0 0 0];

サインインしてコメントする。

### 採用された回答

Geoff Hayes 2014 年 5 月 4 日
Mohammad - Please edit your above code so that it is a little more readable. Comments would definitely help. Please also describe the problem you are facing with the above - why isn't it doing what you want it to do? What is it supposed to do?
Some things that I noted:
The condition on your first if statement is invalid: the matrix e is not yet defined so will error out with the Undefined function or variable e. As well, what is the intent of that condition in trying to check to see if a matrix is greater than or equal to zero? Are you checking to see if all elements in it are greater than or equal to zero (and why?)?
It may be a good idea to put a breakpoint in the code - at the first line - and then step through the code to see if what is happening (at each line) is exactly what you want. For example, a matrix b is defined but unused - why? The matrix c is a 2x4 matrix then on the next line is set to zero which will cause a division by zero problem at the line
f = (e.^2)./c;
and so f is a 2x4 matrix of NaN values. The matrix a is 10x2 and yet the code
for j = 2:size(a,3)+1
is trying to get the size of third dimension (which doesn't exist!), returning the value of one. Why?
What problem is the above code trying to solve? It may be a good idea to comment the code so that it is clear to all what the intent of each line is.
##### 3 件のコメント表示非表示 2 件の古いコメント
Geoff Hayes 2014 年 5 月 6 日
Awesome!

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by