load('matlab_v1.mat')
load('matlab_v2.mat')
[r,c]=size(v1);
res=zeros(size(v1));
t=0;
for i=1:c
for ii=1:r
a=v1(ii,i);
b=v2(ii,i);
if a
t=0;
end
if b
t=1;
end
Res(ii,i)=t;
end
end
%explanation:
%When v1=1 then i set=0 Res until the end
%When v2=1 then i set=1 Res until the end
it's possibile to velocize it avoid loop (using vectorizing or other)

댓글 수: 2

if a
t=0;
end
if b
t=1;
end
It is possible that a is 0 and b is also 0. In that case, t will remain unchanged. That makes it more difficult to vectorize, since you have to copy the previous t .
shamal
shamal 2024년 8월 30일
yes..that's correct

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

 채택된 답변

Walter Roberson
Walter Roberson 2024년 8월 30일

0 개 추천

load('matlab_v1.mat')
load('matlab_v2.mat')
res = nan(size(v1));
res(v1 ~= 0) = 0;
res(v2 ~= 0) = 1;
res = reshape(fillmissing( res(:), 'previous', 'EndValues', 0), size(res));

댓글 수: 2

shamal
shamal 2024년 8월 30일
thank..very good...is more speed
shamal
shamal 2024년 10월 20일
편집: shamal 2024년 10월 21일
using this data your code is not correct
your solution is not equal to this my code
[r,c]=dimensione(v1);
res=zeros(dimensione(v1));
t=0;
per i=1:c
per ii=1:r
a=v1(ii,i);
b=v2(ii,i);
se un
t=0;
FINE
se b
t=1;
FINE
Res(ii,i)=t;
FINE
FINE

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

추가 답변 (1개)

Jatin
Jatin 2024년 8월 30일

0 개 추천

Hi Luca,
As per my understanding, you are trying to fill a new logical array “res” which is assigned a value of 0 if the logical array v1 has a value 1 and assigned a value 1 if the logical array v2 has a value 1.
The above requirement can be easily vectorized in MATLAB using logical indexing of arrays. Refer the script below:
res = zeros(size(v1));
% Logical indexing to set values based on conditions
res(v2) = 1; % Set to 1 where v2 is true
The assignment of res using v1 is not necessary because res is already initialized to all zero so assignment of “0” is redundant here.
Note: If you are trying to assign the values of res as a switch where you keep on assigning a value 1 until you encounter v1 = 1” and assign value 0 until you encounter v2 = 1”. In this case the current assignment results depend on the previous state and hence vectorized assignment cannot be done.
You can refer the documentation below for information on “Array Indexing on logical values”:

댓글 수: 9

shamal
shamal 2024년 8월 30일
my problem is that when I encounter v1=0 write all or....When I encounter v2=1 I write all 1..How would you code it without looping?
Stephen23
Stephen23 2024년 8월 30일
"How would you code it without looping?"
Why?
shamal
shamal 2024년 8월 30일
because I have a lot of data and I thought it could be speeded up easily
Stephen23
Stephen23 2024년 8월 30일
"because I have a lot of data and I thought it could be speeded up easily"
I doubt that doing this without a loop would be faster.
What would make your code faster is fixing the name Res / res.
shamal
shamal 2024년 8월 30일
if it could be vectorized it would be faster but I see that it doesn't work
shamal
shamal 2024년 8월 30일
excuse me but you write "What would make your code faster is fixing the name Res / res."
why uppercase is more slow of lowercase?
Star Strider
Star Strider 2024년 8월 30일
MATLAB is case-sensitive. So ‘res’ is not the same as ‘Res’. They are entirely different variables.
Jatin
Jatin 2024년 8월 30일
"why uppercase is more slow of lowercase?"
Stephen23 pointed out that using "res" instead of "Res" can enhance speed because "res" is already pre-initialized in your code. Since "Res" was not pre-initialized, every time you assign values to it, memory has to be dynamically allocated, which adds overhead. By pre-initializing "res," you avoid this overhead, saving time in your operations.
shamal
shamal 2024년 8월 30일
ok thank you

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2024년 8월 30일

편집:

2024년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by