Column Comparison and Boolean

15 Ansichten (letzte 30 Tage)
Pat
Pat am 21 Feb. 2020
Kommentiert: Jacob Wood am 21 Feb. 2020
Hello,
I am having trouble with a for loop, and I know this is probably super easy but I am not very well versed in MatLab.
I have an array that is 512x193. What I need to do is go through, column by column, comparing 1 and 2, 3 and 4, 5 and 6, etc. until I get to 511 to 512. The result of my comparison will need to be a boolean, so I will wind up with a 256x193 array at the end of this. Below is my code:
responsearray=[]
indexcolumna=0
indexcolumnb=0
responseindex=0
responsearray=0
for responseindex=1:193
for indexcolumna=2:2:512
if (array(indexcolumna,1)>array(indexcolumna,1));
responsearray(responseindex,1)=1
else
responsearray(responseindex,1)=0
end
end
end
Also, I know I am not indexing the columns yet. Just trying to make this one column work first.
What is happening is that I am getting my comparison back as all 0s or all 1s, and I know this isn't correct.
This forum has been very helpful in the past and I know it is probably something very simple. I did try to search the forum for a similar case.

Antworten (1)

Jacob Wood
Jacob Wood am 21 Feb. 2020
Matlab is pretty good at this one :)
responsearray = A(:,2:2:end)>A(:,1:2:end);
What we are doing here is creating an array with all the rows and every other column (2,4,6...) and comparing each element to another array consisting of all the odd columns. The return is an array of logicals where 1 represents true and 0 represents false.
If we wanted to look at your code I think the issue might be that the comparison is done between the same element twice:
array(indexcolumna,1)>array(indexcolumna,1)
Perhaps you are looking for this instead?
array(indexcolumna-1,1)>array(indexcolumna,1)
  2 Kommentare
Pat
Pat am 21 Feb. 2020
Yes, I think you are right. That was so easy...
So I did try the responsearray comparison you mentioned, and I got this error:
"Error using > Matrix dimensions must agree.
Error in Test_cell (line 75)
responsearray=array(:,2:2:end)>array(:,1:2:end);"
Not sure why it is saying the matrix dimensions don't agree...
Jacob Wood
Jacob Wood am 21 Feb. 2020
Oh oops, indexed backwards. We want to compare row2 to row1, row4 to row3, etc. Not column1 to column2.
responsearray = A(2:2:end,:)>A(1:2:end,:);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by