小编use*_*119的帖子

在Matlab中操作没有for循环的矩阵

我在Matlab工作.我在一个名为im1的矩阵中有一个彩色图像.我需要将所有黑色像素都设为白色而不改变其他像素.如果没有for循环,我怎么能这样做?此代码需要很长的10秒才能在大图像上执行.

for i=1:h
for j=1:w
    if im1(i,j,:)==0
        im1(i,j,:)=255;
    end
end
end
Run Code Online (Sandbox Code Playgroud)

performance matlab image-processing matrix vectorization

2
推荐指数
1
解决办法
148
查看次数

倒转列表的代码更简单,更快捷?

我编写了用于反转每个节点中包含单词的双向链表的代码,它完全正常.我的老师说这个算法很难理解,整个代码可以提高效率(减少开销和内存消耗).我可以对代码/反转算法做出哪些更改?还有一种方法我可以输入句子,而不必事先询问单词的数量?这是代码:

#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct NODE
{
    char *item;
    struct NODE *next;
    struct NODE *prev;
}NODE;
void Insert(char data[],NODE **List)
{
    NODE *temp,*last;
    last=(*List);
    temp=(NODE*)malloc(sizeof(NODE));
    temp->item=(char*)malloc(strlen(data));
    temp->item=data;
    temp->next=NULL;
    temp->prev=NULL;
    if((*List)->item==NULL)
        (*List)=temp;
    else
    {
        while(last->next!=NULL)
            last=last->next;
        temp->prev=last;
        last->next=temp;
        last=temp;
    }
}
void Reverse(NODE **List)
{
    int flag1=0;
    NODE *temp,*temp1,*last,*flag;
    temp1=(NODE*)malloc(sizeof(NODE));
    last=(*List);
    while(last->next!=NULL)
        last=last->next;
    temp=last;
    while(temp->prev!=NULL)
    {
        temp1->item=temp->item;
        temp1->next=temp->next;
        temp1->prev=temp->prev;
        temp->next=temp->prev;
        temp->prev=temp1->next;
        temp=temp->next;
        if(flag1==0)
        {
            flag1++;
            flag=temp;
        }
    }
    temp1->item=temp->item;
    temp1->next=temp->next;
    temp1->prev=temp->prev;
    temp->next=NULL;
    temp->prev=temp1->next;
    (*List)=flag->prev;
    free(temp1);
};
void display(NODE *List)
{ …
Run Code Online (Sandbox Code Playgroud)

c algorithm linked-list

1
推荐指数
1
解决办法
156
查看次数