Logical Input for embedded if loop.

조회 수: 13 (최근 30일)
Andrew Hawkins
Andrew Hawkins 2017년 5월 30일
편집: Guruprasad Madhale Jadav 2018년 6월 6일
Below is the assignment: 1. Create a 4 x 8 matrix of randomly generated numbers. 2. Loop through all rows and columns, and test whether each element is greater than 0.5. 3. Report the results of the test along with the value of the matrix ele- ment and its row-column position. For example, your Matlab script should print The 3rd row and 8th column has a value of 0.42345 and is not bigger than 0.5. 4. Make sure to add exceptions to print out 1st, 2nd, and 3rd, instead of 1th, 2th, and 3th. 5. Put this code into a separate function that you can call from the com- mand line with two inputs, corresponding to the number of rows and the number of columns of the matrix.
The following code has an issue in the if loop where it states that the input and output for a logical comparator are not balanced. However, I have added 1 one on each side. A troubleshoot would be appreciated
%4.7 Excercises Cohen text
rand_matrix_A = rand ([4,8]);
[row,col,v] = find (rand_matrix_A > 0.5);
ind_row_col= horzcat (row, col);
for i = 1 : size (ind_row_col, 1);
% exceptions for row index
if ind_row_col(i,1)== 1 or ind_row_col(i,2)== 1;
num_modifier1 = 'st';
disp (['The ' num2str(ind_row_col(i,1)) num_modifier1 ' row and ' num2str(ind_row_col (i, 2)) num_modifier1 ' column has a value of ' num2str(rand_matrix_A (ind_row_col(i,1), ind_row_col (i, 2))) ' and is not bigger than 0.5'])
end
if ind_row_col(i,1)== 2 or ind_row_col(i,2) == 2;
num_modifier2 = 'nd';
disp (['The ' num2str(ind_row_col(i,1)) num_modifier2 ' row and ' num2str(ind_row_col (i, 2)) num_modifier2 ' column has a value of ' num2str(rand_matrix_A (ind_row_col(i,1), ind_row_col (i, 2))) ' and is not bigger than 0.5'])
end
if ind_row_col(i,1)== 3 or ind_row_col(i,2) == 3;
num_modifier3 = 'rd';
disp (['The ' num2str(ind_row_col(i,1)) num_modifier3 ' row and ' num2str(ind_row_col (i, 2)) num_modifier3 ' column has a value of ' num2str(rand_matrix_A (ind_row_col(i,1), ind_row_col (i, 2))) ' and is not bigger than 0.5'])
end
disp (['The ' num2str(ind_row_col(i,1)) 'th row and ' num2str(ind_row_col (i, 2)) 'th column has a value of ' num2str(rand_matrix_A (ind_row_col(i,1), ind_row_col (i, 2))) ' and is not bigger than 0.5'])
end

채택된 답변

Jan
Jan 2017년 5월 30일
편집: Jan 2017년 5월 30일
The or() command is used incorrectly:
if ind_row_col(i,1) == 1 or ind_row_col(i,2) == 1;
Better:
if ind_row_col(i,1) == 1 || ind_row_col(i,2) == 1
or
if or(ind_row_col(i,1) == 1, ind_row_col(i,2) == 1)
or leaner:
if any(ind_row_col(i,1:2) == 1)
(You see: no trailing semicolon)
There are no "if loops", neither in Matlab nor in any otehr programming language.
input and output for a logical comparator are not balanced
Better post a complete copy of the message. A rough rephrasing does not allow to understand, what you see. Perhaps this message means, that the indentation is poor. This is a hint only and not an error. Mark the code an hit Ctrl-I. Does the warning disappear?
  댓글 수: 2
Andrew Hawkins
Andrew Hawkins 2017년 5월 30일
I received the following message despite the edit you described above. I may have mistaken the error in my original post.
Error using |
Too many input arguments.
Jan
Jan 2017년 5월 31일
편집: Jan 2017년 5월 31일
Please post the complete error message and show us the failing line of code. The code you have posted does not contain a | , so how could we guess, what's going on?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guruprasad Madhale Jadav
Guruprasad Madhale Jadav 2018년 6월 6일
I have attached a file with the working example. Chears :)
  댓글 수: 1
Guruprasad Madhale Jadav
Guruprasad Madhale Jadav 2018년 6월 6일
편집: Guruprasad Madhale Jadav 2018년 6월 6일
to call it as a function add the following code on the top of the script and remove the assigned constants (in this case file name is Chapter4a.m)
function [] = filename(rows, columns) % code

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by