MATLAB For Loops Homework: Solving Arbitrary Matrices

In summary: You're supposed to do something with the if statement, but it's not clear what. You might want to try something like this:if (arr(i) < min_a)min_a = arr(i);end ifAfter running this code, you should be able to see that the second for loop works just fine.In summary, the student is trying to solve an equation that asks for a matrix that can be solved for any x and y value. The student is having trouble with the first part of the equation, which asks for an array with the same size as the input matrix. The code does not work because the number of elements in the array and the input matrix must
  • #1
Feodalherren
605
6

Homework Statement


asd.png


Homework Equations


-

The Attempt at a Solution


I've been trying several different ways but I just can't seem to figure this one out. It seems to me like I will need nested for loops, which we haven't learned yet. The way I read the question is that this program needs to be able to solve any arbitrary matrix, which means that it can have x rows and y columns. So I am completely perplexed.

I tried writing a for loop that extracts all the rows into one row:
-----------
clc;clear

a=input('enter a matrix: ')
HowBig=size(a);
rows=HowBig(1,1);
columns=HowBig(1,2);

for j=1:rows
a(j)=[a(j,:)]
end
----------------

But MATLAB refuses to run this code because

In an assignment A(I) = B, the number of elements in B and I must be the same.


after running this program I wanted to do another loop like this:

-----------------------------------------
for i=1:length(a)
if i==length(a)
break
end

if a(
1,i)<=a(1,i+1)
min_a=a(1,i);
else
min_a = a(1,i+1);
end %if

end %for


disp(num2str(min_a))
------------------------------------------


Obviously something is really wrong here but I am completely stuck. The second line of code seems to run perfectly well, I just cannot figure out what to do with the first part. Is there any way to pull out the whole Matrix into a single row vector?
 
Physics news on Phys.org
  • #2
I don't think you need to nest for loops for this.
What you do need to do is work out the logic in advance.

Testing:
Code:
> a=magic(3)
a =

   8   1   6
   3   5   7
   4   9   2
... the magic squares are useful test matrixes.

Lets try out that first instruction, pretend that j=1 for starters.
Code:
[/SIZE]
> a(1)=[a(1,:)]
error: A(I) = X: X must have the same size as I
... well yes, naturally, because
Code:
> size(1)
ans =

   1   1

> size(a(1,:))
ans =

   1   3
... what were you trying to do?
Bearing in mind that you had already used "a" as the input matrix ... assigning a(something)=something-else will change the matrix.
Probably best not to do that.

I don't follow your reasoning for the second set of code.
Sart by working out the logic of the program - go through what the program has to do one step at a time.
 
  • Like
Likes Feodalherren
  • #3
Feodalherren said:

Homework Statement


asd.png


Homework Equations


-

The Attempt at a Solution


I've been trying several different ways but I just can't seem to figure this one out. It seems to me like I will need nested for loops, which we haven't learned yet. The way I read the question is that this program needs to be able to solve any arbitrary matrix, which means that it can have x rows and y columns. So I am completely perplexed.

I tried writing a for loop that extracts all the rows into one row:
-----------
clc;clear

a=input('enter a matrix: ')
HowBig=size(a);
rows=HowBig(1,1);
columns=HowBig(1,2);

for j=1:rows
a(j)=[a(j,:)]
end
----------------

But MATLAB refuses to run this code because

In an assignment A(I) = B, the number of elements in B and I must be the same.
You're really off track here. You are supposed to be working with an array, which is a matrix with one row. In your 3rd line of code you are setting HowBig to the singel value returned by size, but in your 4th and 5th line, you're assuming that HowBig is a two-dimensional array.

There is no need for doing anything with rows and columns as you're doing in the 4th and 5th lines.

The basic algorithm should be something like this:
Code:
Initialize an array with some numbers.
Set min_a to arr(1)
For each element in the array
   if arr(i) < min_a
      min_a = arr(i)
  end if
end for
Feodalherren said:
after running this program I wanted to do another loop like this:

-----------------------------------------
for i=1:length(a)
if i==length(a)
break
end

if a(
1,i)<=a(1,i+1)
min_a=a(1,i);
else
min_a = a(1,i+1);
end %if

end %for


disp(num2str(min_a))
------------------------------------------


Obviously something is really wrong here but I am completely stuck. The second line of code seems to run perfectly well, I just cannot figure out what to do with the first part. Is there any way to pull out the whole Matrix into a single row vector?
 
  • Like
Likes Feodalherren
  • #4
------------------
L=length(a);

min_a=a(1,1);

for i=1:L
if a(1,i) <= min_a
min_a = a(1,i);
end
end

min_a

------------------

This worked for the most part. But even though the problem says array, my code failed one test with this message:

----------------
Fail
%%
A = [5 2 4;-6 7 -10;4 -4 8];
assert(MyMin(A)==-10,'Failed matrix test')

------------------

So it looks like I do indeed have to adjust for matrices which brings me back to my initial problem.
As for what I was trying to do: I was trying to figure out a way of testing ALL values in a matrix against each other.
 
  • #5
Feodalherren said:
------------------
L=length(a);

min_a=a(1,1);

for i=1:L
if a(1,i) <= min_a
min_a = a(1,i);
end
end

min_a

------------------

This worked for the most part. But even though the problem says array, my code failed one test with this message:

----------------
Fail
%%
A = [5 2 4;-6 7 -10;4 -4 8];
assert(MyMin(A)==-10,'Failed matrix test')

------------------

So it looks like I do indeed have to adjust for matrices which brings me back to my initial problem.
As for what I was trying to do: I was trying to figure out a way of testing ALL values in a matrix against each other.
Why are you using a 2-D array? You are making it more complicated than it needs to be. The problem statement uses a 1-D array.
 
  • #6
I'm not using it. I plug my code into a thing called Cody and it checks for me. I'm guessing my professor made a mistake in the problem statement as Cody does check for matrices. So Cody plugged in the 2D array.
 
  • #7
I don't know anything about Cody - what it is or what it does. Can't you write your MATLAB code without Cody? How can the problem statement be wrong? It is what it is, and the example input is a one-D array. You can use any text editor, like NotePad or NotePad++ to write an M-file.
 
  • #8
This should work.
Code:
a = [5 0 -3 2 -6 8]
L=length(a);
min_a=a(1);

for i=2:L
   if a(i) <= min_a
     min_a = a(i);
   end
end

min_a;

After this code runs, it should display -6.

Since your task is to write an M-file, you need to write a function that returns the smallest value in the array.
 
  • #9
Cody is a program that tests MATLAB codes. It's automatic. You submit a code online and Cody tests it and let's you know which tests you passed and didn't pass. The problems for Cody are written by the professor who also does the tests. In this case there was a mistake in the problem statement.
 

1. What is a for loop in MATLAB?

A for loop in MATLAB is a programming construct that allows for repeated execution of a set of code based on a specified number of iterations or a condition. It is used for efficient handling of repetitive tasks in MATLAB.

2. How do I create a for loop in MATLAB?

To create a for loop in MATLAB, you need to define the starting and ending values for the loop, as well as the increment or decrement value. Then, use the for keyword followed by a variable name and the in keyword, followed by the range of values to be iterated over, and end the statement with the end keyword.

3. How can I solve arbitrary matrices using for loops in MATLAB?

To solve arbitrary matrices using for loops in MATLAB, you can use the for loop to iterate over the rows and columns of the matrix and perform the necessary computations on each element. You can also use nested for loops to access and manipulate individual elements of the matrix.

4. Can I use a for loop to solve matrices of any size in MATLAB?

Yes, you can use a for loop to solve matrices of any size in MATLAB. However, as the size of the matrix increases, the time taken to execute the loop also increases. Therefore, it is recommended to use vectorized operations instead of for loops for larger matrices to improve performance.

5. Are there any alternatives to for loops for solving matrices in MATLAB?

Yes, MATLAB offers alternatives to for loops for solving matrices, such as vectorized operations and built-in functions like sum() and prod(). These alternatives are more efficient and faster than for loops and should be used for larger matrices.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
943
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Back
Top