Column Comparison and Boolean
15 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Jacob Wood
2020-2-21
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 个评论
Jacob Wood
2020-2-21
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,:);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!